C#

Learning All Class Members in C#

Programmingempire

In this post on Learning All Class Members in C#, I will talk about the different kinds of members that a class in C# can have. Like other object-oriented programming languages, a C# class can have data members and methods that operate on those data members. However, a C# class can have several other types of members apart from data members and member functions.

Class Members in C#

Basically, a C# class can contain the following members.

  1. Fields
  2. Methods
  3. Constructors
  4. Destructor
  5. Properties
  6. Indexers
  7. Events
  8. Nested Types

Field

Basically, a field represents a data member. In other words, a field holds a data value. Additionally, a field can represent an instance variable or a static variable. Also, we can have a constant field as well as a readonly field in a class.

Further, it should be noted that a const field is initialized at the time of declaration and then it cannot change its value. In contrast, a readonly field can be assigned its value in the constructor as well as at the time of declaration, and then its value is not changed. The following example shows the usage of all of these types of fields.

class MyClass
    {
        int a, b; // instance variables of the class
        static int counter = 0; // a static field
        const double pi = 3.14; // a const field
        readonly string title = "Demonstration of Fields"; // a readonly field
    }

Methods

While the instance variables represent attributes of an object, the methods represent their behavior. basically, a method implements some kind of functionality. Moreover, we can define both static and non-static methods in a class. The static methods are invoked with the help of class names, whereas non-static methods are called using objects. As an illustration, consider the following example.

class MyClass
    {
        public int a, b; // instance variables of the class
        static int counter = 0; // a static field
        public int findsum() // a non-static method
        {
            return a + b;
        }
        public static int GetCount() //a static method
        {
            counter++;
            return counter;
        }
    }

Accordingly, we can invoke these methods as shown below. Evidently, we create an object of the class with the name ob to access the instance variables and the instance method. While the static method is called using the name of the class.

class Program
    {
        static void Main(string[] args)
        {
            MyClass ob = new MyClass();
            ob.a = 89;
            ob.b = 76;
            // Calling a non-static method
            Console.WriteLine($"Sum of {ob.a} and {ob.b} is {ob.findsum()}");
            // Calling a static method
            Console.WriteLine($"The value of counter is {MyClass.GetCount()}");
        }
    }

Output

Demonstrating the Methods of a Class
Demonstrating the Methods of a Class

Constructor

Likewise, a class can have constructors which are special functions having the same name as the name of the class and no return type. The sole purpose of a constructor is to initialize the fields of the class. Also, we can have both instance constructors as well as a static constructor in a class as shown in the following example.

class MyClass
    {
        public int a, b; // instance variables of the class
        static int counter = 0; // a static field

        public MyClass() // default instance constructor
        {
            a = b = 10;
        }

        public MyClass(int a, int b) // parameterized instance constructor
        {
            this.a = a;
            this.b = b;
        }
        static MyClass() //static constructor
        {
            counter = 1;
        }
  }

Destructor

Basically, the destructor is also a special function whose purpose is to destroy the object when it is no longer in use. However, the Garbage Collector of .NET Framework implicitly invokes the destructor. For example, the following code shows a destructor.

~MyClass()
 {

 }

Properties

Suppose a class has its data members as private fields that are not accessible from outside the class. In such a case, properties provide a mechanism to set and get these values. meanwhile, the following code shows an example of a property corresponding to the field a.

 class MyClass
    {
        int a;
        public int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
    }

Indexer

Besides properties, a class can also have indexers that contain get and set accessors. Basically, an indexer allows an object to be accessed like an array as shown in the following example.

class MyClass
    {
        int[] arr= { 1, 2, 3 };
        public int this[int i]
        {
            get
            {
                return arr[i];
            }
            set
            {
                arr[i] = value;
            }
        }
   }

Events

An even can also be a member of a C# class and represents the occurrence of something like a keypress, a mouse click, or closing a window. As an illustration, consider the following example.

public delegate void del();
class MyClass
{
    public event del MyEvent;
}

Nested Types

In fact, a class can also include another class as its member. For this reason, the member class is also called the Nested Class. For further information on nested classes click here. Meanwhile, an example of a nested class is given below.

class MyClass
{
    int a, b;
    class MyNestedClass
    {
        int x, y;
    }
}

Summary

In this article on Learning All Class Members in C#, you learned about various types of members that a C# class may include. Also, examples of each type of member are also provided to demonstrate each of the corresponding class members.


programmingempire

You may also like...