[WEBINAR] Application Acceleration: Faster Performance for End Users

Class members

Definition - 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
This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
"Techopedia" on Twitter

Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Free Whitepaper: The Path to Hybrid Cloud
Free Whitepaper: The Path to Hybrid Cloud:
The Path to Hybrid Cloud: Intelligent Bursting To Amazon Web Services & Microsoft Azure
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.