What Does Class members Mean?
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:
- Constants representing constant values
- Fields representing variables
- Methods providing services like calculation or other actions on its members
- Properties that define the class features and include actions to fetch and modify them
- Events generated to communicate between different classes /objects
- Indexers that help in accessing class instances similar to arrays
- Operators that define semantics when used in expressions with class instances
- Instance constructors to initialize members of class instances
- Static constructor to initialize the class itself
- Destructors to execute actions necessary to be performed before class instances are discarded
- Types that are local to the class (nested type)
Class members can be specified individually with any one of the following accessibility:
- Public –accessible by any code within current or external assembly
- Protected –accessibility within the same class or its derived class
- Private –accessibility within the same class
- internal –accessibility within current assembly
- protected internal –accessibility within current assembly or classes derived from containing class
Unlike Java where the default accessibility level of a class member is public, it is private in C#.
Techopedia Explains Class members
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:
- Except for constructors and destructor, all the other members need to have name different from class name
- Names of constant, field, property, event or type must be unique within the class
- Multiple methods can be declared with same name but should differ in their signature
- Signature of indexer and operator must be unique among the ones declared within the class
- Class member cannot have reserved signatures which are member names reserved for properties, events, indexer and ‘Finalize’
- Type of a member, parameter and return value of a method , delegate or indexer should be accessible as the member itself
- User defined operators must have accessibility level as public