Lesson 8 – Objects and Classes in C#

C# is 99.9% object-oriented language. Apart from the primitive data typed that you saw in lesson 2, everything in C# happens within a class. In this lesson, we will study the concept of objects and classes in C#.

Class in C#

A class is basically a blueprint for the object. A class can contain methods, variables, properties, etc. Anything that has some attribute and can perform any function can be implemented as a class in C#. Let’s create a fictional class “Computer”. The class will have two member variables: memory and space, and one method “SwitchOn”. Let’s see how to create such a class in C#.

    public class Computer
    {
        public int memory = 16;
        public int space = 500;

        public void SwitchOn()
        {
            Console.WriteLine("Computer has been Switched On");
        }
    }

Objects

A class has no physical existence in a computer’s memory. It is just a map. A class is actually implemented via object. An object exists in the physical memory of the computer. To create an object of a class, you have to use the keyword “new” followed by the class name and opening and closing parenthesis. In the following script, we create an object of the “Computer” class inside the “Main” method and call the “SwitchOn” method using the object. To access class methods/functions, a dot operator is used. You can see in the above script that “SwitchOn” function is not static, which means that we have to use an object of a class to call this function. Look at the following script:

   class Program
    {
        static void Main(string[] args)
        {
            Computer c = new Computer();
            c.SwitchOn();
        }

    }

And here is the complete code for the class and object:

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            Computer c = new Computer();
            c.SwitchOn();
        }

    }

    public class Computer
    {
        public int memory = 16;
        public int space = 500;

        public void SwitchOn()
        {
            Console.WriteLine("Computer has been Switched On");
        }
    }
}

The output of the above script is as follows:

Computer has been Switched On

Class Constructor

A constructor is a method with the same name as the class name. A constructor has no return type. It is automatically called when you create an object of a class. You can pass values to a constructor when you create an object of a class. Let’s take a look at a simple example of a class constructor.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            Computer c = new Computer(32, 1000);
            Console.WriteLine(c.memory);
            Console.WriteLine(c.space);
            c.SwitchOn();
        }

    }

    public class Computer
    {
        public int memory;
        public int space;

        public Computer(int memory, int space)
        {
            this.memory = memory;
            this.space = space;
        }
        public void SwitchOn()
        {
            Console.WriteLine("Computer has been Switched On");
        }
    }
}

In the code above, the constructor of the Computer class is being used to initialize the member variables: memory and space. The output of the above script is as follows:

32
1000
Computer has been Switched On

What’s Next?

In this lesson, you started exploring objects and classes. In the next lesson, you will see how to implement inheritance between classes in C#.