- 'ref' and 'out' parameters:
In C#, parameters are typically passed by value, meaning that a copy of the variable is passed to the method. However, 'ref' and 'out' parameters allow you to pass variables by reference instead of by value.
The 'ref' keyword is used when you want to pass a variable by reference, which means that any changes made to the variable inside the method will be reflected outside of the method as well.
The 'out' keyword is similar to 'ref', but it is used when you want to pass a variable by reference and also guarantee that it will be assigned a value inside the method.
- Constant values:
Constant values, also known as constants, are variables that cannot be modified once they are assigned. In C#, you can declare a constant variable using the 'const' keyword.
For example:
const int MAX_VALUE = 100;
In this case, the variable MAX_VALUE is assigned the value 100 and cannot be changed later in the program.
Constants are useful when you need to define values that should never change, such as mathematical constants or configuration settings.
- Enumerations:
Enumerations, or enums, are a way to define a set of named constants. Enums are commonly used to represent a fixed set of values that a variable can take.
For example:
enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
Now, a variable of type 'Days' can only have one of the predefined values from the enum.
Enums provide an improved code readability as they give meaningful names to a set of related values.
- Inheritance and Polymorphism:
Inheritance is a fundamental concept in object-oriented programming where a class can inherit the properties and methods of another class. The class that is being inherited from is called the base class, and the class that inherits from it is called the derived class.
Polymorphism is the ability of an object to take on different forms depending on its context. In C#, polymorphism is achieved through inheritance and method overriding.
By using inheritance and polymorphism, you can create a hierarchy of classes that share common characteristics and behaviors, while also allowing each class to have specific implementations.
- The root of all classes:
In C#, the root of all classes is the Object class. The Object class is the most basic class in C#, and all other classes implicitly inherit from it.
The Object class provides a set of common methods, such as ToString(), GetHashCode(), and Equals(), which can be used with any object in C#.
By having all classes inherit from the Object class, it allows for a consistent behavior across different types of objects and enables features like polymorphism and type casting.
- Creating derived classes:
To create a derived class, you use the syntax 'class DerivedClassName : BaseClassName'. This means that the derived class will inherit all the members (properties, methods, etc.) of the base class.
For example:
class Animal {
// base class
}
class Dog : Animal {
// derived class
}
In this example, the class Dog is derived from the base class Animal. This means that the Dog class will have all the members of the Animal class and can also add its own unique members.
By creating derived classes, you can reuse code from the base class, add new functionalities specific to the derived class, and create a hierarchy of classes for more organized and modular code.