Understanding Different Class Member Modifiers in C# (C Sharp)C# ensures data encapsulation by using modifiers. These modifiers can be classified into access modifiers (public, private, protected, internal), class member modifiers (const, event, override, extern, static, readonly, abstract, virtual) and other additional modifier called sealed.
This article will help you in understanding the purpose and usage of class member modifiers. The class member modifiers are used to modify the class members behavior. As mentioned above, C# supports eight different class member modifiers which are explained below: Const: If you want to prevent a member variables value from getting altered, use const modifier. This modifier can be applied only on class variables and not on its methods. Here is an example: public class
testClass{ Event: Notification of an event occurrence between classes is achieved using the event modifier. Event modifier is closely related with delegates. While coding, declare a delegate and define an event for that delegate. You can declare a delegate as follows: public delegate
void MyDelegate(); Override: When you want to extend and implement an abstract or virtual method, use override modifier. For example, car is an abstract class with engineConfiguration as its method. If any class inherits from car, it has to override engineConfiguration method. abstract
class car { Extern: If
you want to use a method or function that is implemented outside your
c# code, then you can do it by using extern modifier. Here is an example: public void
callExternFunction() { External
method should not include method body in your code Static: In normal scenarios, you will access class members using instance of the class. Each member will be initialized and maintained separately for each instance of the class. What if you want a member to be unique to the class and its value retained class level rather than object level? In that case you can use the modifier static. Assume that you want to know the number of instances created for a class, how will you achieve it? public class
testStatic{ Output will be 2. Also note that the static variable countInstances is called using the class name and not the object name. Readonly:
You might require a constant in your code which cannot be evaluated at
compile time or it cannot be initialized during declaration itself. In
those cases, you cannot use const modifier. However, you can use the class
member modifier readonly. ReadOnly variables can be initialized once either
during its declaration or in the class constructor and they cannot be
altered further. Here is an example: The output will be 100. Similar to constants, you cannot change the value of a readonly variable. If you include the code readOnlyVar = 500; inside your Main() method, then the compilation error CS0198 stating A static readonly field cannot be assigned to (except in a static constructor or a variable initializer) will be thrown. Remember that this modifier can be applied only on class member variables and not on its methods. Abstract: Use abstract modifier on a property or method of a class if you want to implement the class only in the derived class and not in the parent class. Example given for the modifier override will cover usage of abstract modifier as well. Ensure that you follow the guidelines given below when you are using abstract modifier on a class member: You
can declare a class member as abstract, only if the class is defined as
abstract Virtual: Assume that there is a method which is implemented in base class and overridden in derived class. If you want to trigger the method by identifying the instance type at run time rather than at compile time, then you can use the Virtual modifier, which can be applied to both properties as well as methods of a class. Heres an example: class baseClass
{ Both baseObj.testMethod() and derObj.testMethod() will call the overridden method, this is because baseObj is referring to derived class object. Ensure that virtual modifier should not be used along with abstract or static or override modifiers.
|