Signup/Sign In

C# Classes and Objects

Posted in Programming   LAST UPDATED: OCTOBER 18, 2021

    In the previous tutorial, we covered loops in C# and in this tutorial we will move on to a very important topic Classes and Objects. Since the beginning of this C# tutorial, we have been using a class in each program. So, in this tutorial, we will see in detail about classes and objects and how they are fundamental to C#.

    C#: What is a Class?

    A class is a template from which we create objects. A class is a blueprint while the object is a functional instance of it. In simpler words we can say that a class is a data type which has its own methods and properties, which can be used by creating an instance or object of it. It is created using the keyword class followed by class name and the class body. The class can have data members, methods, constructors, etc.

    Here is an example of class definition:

    <access modifier> class class_name
    {
        // this is the class body
        // Declare instance variables
        <access modifier> <data type> variable_name; 
        // then class methods
        <access modifier> <return type> method_name()
        {
            // This is the method body
        }
    }

    The elements of the class declaration are:

    1. Access Modifiers: This determines the visibility of a variable/attribute or a method of a given class. It can be public, private, protected, internal or protected internal. This determines whether an attribute or a method of a class can be accessed from outside of class or not. The default access modifier for a class is internal and for its data members, it is private. We will cover this in details in our upcoming tutorials.

    2. Class Keyword: The class keyword is used for declaring the type class.

    3. Class Name: The name of the class follows the class keyword. The name of the class must be a valid C# identifier name.

    4. Class Body: Here the behavior and data are defined. Properties, methods, and events on a class are collectively referred to as class members.


    C#: What is an Object?

    An object is areal world entity having a state (data) and behavior (functionality). If you are confused about class and objects, watch this Youtube Video to clear all your doubts: Object-Oriented Programming Explained. It is a runtime entity hence created at runtime using the new keyword.

    Here is an example of an object created using the new keyword:

    Studytonight s = new Studytonight();    //creating object of Studytonight class
    

    In the code example above, we have declared a variable s of type Studytonight and then we have assigned a new instance of Studytonight class to it, which is created using the new keyword.

    When an object is created, enough memory is allocated on the managed heap for that specific object, and the variable(in the code example above, s is the variable) holds only a reference to the location of said object.


    Time for an Example

    Let's take a simple code example where we will create a class and will then create an object of it.

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        public class Config
        {
            public int RAM;
            public string processor;
            public bool touch;
        }
    
        class Program
        {
            public static void Main(string[] args)
            {
                Config c = new Config();
                c.RAM = 8;
                c.processor = "i7";
                c.touch = true;
                Console.WriteLine("RAM: "c.RAM +" | Processor: "+ c.processor +" | Touch Screen: "+ c.touch);
            }
        }
    }

    Output:

    RAM: 8 | Processor: i7 | Touch Screen: True

    This program consists of two classes: Config and Program. Inside the Program class, the Main() method creates an instance of Config class assigned to a variable c. Then the code within the Main() method accesses the instance variable associated with c, assigning values to its attributes and then using those values.

    If you observe the above example closely, we have used the public modifier for one of our class as well as for its data members, as data members are private by default. If you don't use public modifier then you won't be able to access the data members outside the class. Therefore, data members are made public.

    Let's take another example where we will be displaying values of class attributes/variables using a method:

    Filename: Program.cs

    using System;
    
    namespace Studytonight
    {
        public class Config
        {
            public int RAM;
            public string processor;
            public bool touch;
            // function to set value to class attributes
            public void setValues(int r, string p, bool t)
            {
                RAM = r;
                processor = p;
                touch = t;
            }
            // function to display value of class attributes
            public void display()
            {
                Console.WriteLine("RAM: "c.RAM +" | Processor: "+ c.processor +" | Touch Screen: "+ c.touch);
            }
        }
    
        class Program
        {
            public static void Main(string[] args)
            {
                Config c = new Config();
                c.setValues(8,"i7",true);
                c.display();
            }
        }
    }

    Output:

    RAM: 8 | Processor: i7 | Touch Screen: True

    In the above example, we have created two methods setValues() and display(). The setValues() method expects 3 arguments to be passed when it is called(this is known as the signature of the method). On the other hand, we have created the object of the Config class and using the object, we are calling the class methods.

    This is how we can create and use classes and objects in C#. Please refer our next tutorial for C# Methods to have a better understanding about methods.

    You may also like:

    About the author:
    Subject Matter Expert of C# Programming at Studytonight.
    Tags:C#C# TutorialC# OOPSC# Class
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS