c sharp

What are Class Members (Fields and Methods) in C#

In C#, a class serves as a model for creating objects with similar attributes and capabilities. The class members are the components that make up a class, including fields and methods. Fields in short are variables that keep the state of the class, and methods are functions that operate on that state. Understanding class members is essential to understand object-oriented programming in C#.

What are Class Members in C#

A class in C# serves as a guide for building objects that include data and behavior. Class members are the variables and functions that are specified within a class and are employed to specify how objects derived from that class will behave. There are two types of class members in C#:

Fields

They are used to store data that is used by the class and its methods. Fields can be either public or private. While private fields are only accessible from within the class, public fields can be accessed from anywhere outside the class. Here’s an example of a class with public and private fields:

using System;

class Identity
{
    // public field
    public string name;
    // private field
    private int age;

    public Identity(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public void SayHello()
    {
        Console.WriteLine($"Hello, my name is {name} and I'm {age} years old.");
    }
}
class myClass
{
    static void Main(string[] args)
    {
        Identity Sam = new Identity("Sam", 25);
        Sam.SayHello();

        Identity Jane = new Identity("Jane", 30);
        Jane.SayHello();
    }
}

Here, in the Identity class, there are two fields defined: name and age. Fields are variables created inside of classes that store the class’s data. In this case, name and age are fields.

Fields may be defined with various access modifiers, such as protected, private, and public. In this case, the name is declared as a public field, which means it can be accessed and modified by any code that has access to an instance of the Identity class. The age on the other hand is declared as a private field, which means it can only be accessed and modified within the Identity class itself.

When a field is declared, it can also be given an initial value. In this case, the name field is initialized in the constructor using the name parameter, while the age field is initialized using the age parameter.

Methods

Methods are functions that operate on the state of the class. They can be used to change the values of the class’s fields or to perform calculations based on the current state of the class. Both public and private methods are possible. Private methods can only be invoked from within the class, but public methods can be called from the outside. Here’s an example of a class with public and private methods:

using System;

class Calculator
{
    // private field
    private int result;

    // public method
    public void Add(int x, int y)
    {
        result = x + y;
    }
    // private method
    private void Clear()
    {
        result = 0;
    }
    public int GetResult()
    {
        return result;
    }
    // entry point of the program
    static void Main(string[] args)
    {
        Calculator calculator = new Calculator();
        calculator.Add(2, 3);
        Console.WriteLine("Result = " + calculator.GetResult());
    }
}

The Calculator class has three class members: a private field result, and three methods Add, Clear, and GetResult. The Add method is a public method that adds two integers and sets the result in the private field result. The GetResult method is a public method that returns the value of the private field result. The Main method is the entry point of the program, which creates an instance of the Calculator class, calls the Add method to add two numbers, and displays the result using the GetResult method.

All of these members, including the field result and the methods Add(), Clear(), and GetResult(), are class members in C#.

Conclusion

Class members, including fields and methods, are the components that make up a class in C#. The state of the class is stored in fields and is used by methods to perform operations. Understanding how to use class members is essential to writing effective object-oriented code in C#.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.