A class in C# can contain many different types of members having different purposes, and accessibility levels. This programming tutorial discusses the various types of class members and how you can work with them in C#.
Looking to learn C# in an online course or class setting? We have a list of the Top Online Courses to Learn C# to help get you started.
What are Class Members? in C#?
Class members in C# are variables, properties, methods, events, and indexers that are declared inside a class or struct. Class members are also known as just members. Class members are the data and functions associated with those classes that are accessible using the dot/b> or b>period operator.
To access class members that are not static, programmers need to create a class instance. On the contrary, to access a static member of a class, developers do not need an instance; instead, you can use the class name to access it.
What are the Different types of Class Members in C#?
Class members can have different levels of accessibility depending on their declared accessibility level and the accessibility level of the containing type. For example, a public member of a public type is accessible to any code that has a reference to the type. A public member of an internal type is only accessible within the assembly in which the type has been declared.
Members of a class in C# are private by default. This implies if programmers do not explicitly specify the accessibility while defining a class member, it can only be accessed within the class. The accessibility of class members in C# can be either public, private, internal, or protected.
When a member is accessible from anywhere within the assembly, it is considered public. You can only access private members of a class from within the same class. Internal members are only accessible within the same assembly, while protected members of a class can be accessed from within derived classes of the same or different assembly.
Class Member Fields in C#
Fields are the most basic type of class member. They are variables declared in the class scope that contain data relevant to the class.
Class Member Methods in C#
Methods are functions that perform actions on behalf of the class. They can take parameters and return values. In other words, a method is an action or operation that a class can perform. The following are the most common characteristics of methods:
- Methods can have access modifiers (public/private/protected/internal).
- Methods can have parameters, and they can return values.
- Methods of a class can be both static and non-static.
Class Member Constants in C#
Constants are fields that have a value that cannot be changed once it is set at the time of their creation.
Class Member Indexers in C#
Indexers are class members that enable you to use an object similar to an array using index.
Class Member Constructors in C#
Constructors are functions or methods of a class that are invoked when the class is instantiated. They have identical names as the class and contain the necessary code to initialize an object. A constructor that does not accept any argument is called the default constructor.
It is also possible to have multiple constructors differing in their signatures, a concept known as constructor overloading. A signature of a method comprises the method name, type and sequence of the parameters, and the return type. Although a constructor can accept parameters, it cannot return any value.
Class Member Properties in C#
Properties are a special type of field that represents data stored in the class, but with additional features. Properties have getters and setters, which allow them to be read from and written to like regular fields. However, properties also allow for extra logic to be executed when they are accessed or modified. This makes them more flexible and powerful than regular fields.
Class Member Destructors in C#
Unlike a constructor, a destructor is a method that cannot accept any parameters and is called when the object of the class goes out of the scope in which it has been created. Note that a destructor implicitly calls the Finalize method.
When a class is no longer needed, the CLR (common language runtime) calls the Finalize method. This method is called on an object when it is about to be destroyed.
Class Member Nested Types in C#
A nested type, as the name suggests, is a type that is contained or housed within another type. Nested types can be used to organize the members of a large class or struct, or to control their accessibility. You can have a class or a struct inside a class. Such a type that is contained inside a class is also known as a nested class or a nested struct. A nested type is declared within the body of its enclosing type, using the same kind of declaration syntax that is used for other members of the enclosing type.
Accessibility Levels of Class Members in C#
In C#, there are four levels of accessibility: public, private, internal, and protected. When a member becomes public, it is accessible from anywhere in the assembly. Private members, unlike public members, may only be accessible inside a class. Internal members are exclusively available inside an assembly, while protected members may be accessed by derived classes in other assemblies.
Final Thoughts on C# Class Members
A good working knowledge of the members of a class and their accessibility levels helps in designing applications that are extendable, maintainable, and robust. In this programming tutorial, we looked at the different types of class members. We also covered accessibility rules for each of these members and how they can be used in your C# code.