Tech moves fast! Stay ahead of the curve with Techopedia!
Join nearly 200,000 subscribers who receive actionable tech insights from Techopedia.
Class members, in C#, are the members of a class that represent the data and behavior of a class.
Class members are members declared in the class and all those (excluding constructors and destructors) declared in all classes in its inheritance hierarchy.
Class members can be of the following types:
Class members can be specified individually with any one of the following accessibility:
Unlike Java where the default accessibility level of a class member is public, it is private in C#.
Class members are initialized in constructors which can be overloaded with different signatures. For classes that do not have constructor, a default constructor that initializes the class members (to default values) will be generated.
Unlike in C++, C# allows a class to inherit from one base class only. However, a class can derive from multiple interfaces but all the interface members have to be implemented. These members of class implementing interface(s) have public visibility by default and cannot have other access modifiers.
All the members of base class except constructors are inherited in derived class. Derived class members can hide the base class member for which ‘new’ keyword has to be used to indicate that base member is not overridden and avoid compiler warnings.
A static class member is a member of static class (that cannot be instantiated nor inherited) that can be accessed only using the class name. Unlike instance member, it has one copy of static member which is initialized before it is accessed for first time and before its static constructor (if any).
Members of abstract class prevent direct instantiation and must be implemented in derived class for it to be used. To prevent a class or class members from inheriting, the class or its members can be declared as ‘sealed’. Class members can be used as ‘compile-time’ constant using ‘const’ modifier and as runtime constants using ‘readonly’ modifier.
While declaring class members in a new declaration space, the following rules apply: