top of page

C# Classes and Objects



A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit. In C#, classes support polymorphism, inheritance and also provide the concept of derived classes and base classes.


Declaration of class

Generally, a class declaration contains only a keyword class, followed by an identifier(name) of the class. But there are some optional attributes that can be used with class declaration according to the application requirement. In general, class declarations can include these components, in order:

  • Modifiers: A class can be public or internal etc. By default modifier of the class is internal.

  • Keyword class: A class keyword is used to declare the type class.

  • Class Identifier: The variable of type class is provided. The identifier(or name of the class) should begin with an initial letter which should be capitalized by convention.

  • Base class or Super class: The name of the class’s parent (superclass), if any, preceded by the : (colon). This is optional.

  • Interfaces: A comma-separated list of interfaces implemented by the class, if any, preceded by the : (colon). A class can implement more than one interface. This is optional.

  • Body: The class body is surrounded by { } (curly braces).

Constructors in class are used for initializing new objects. Fields are variables that provide the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.

Example:

// declaring public class
public class Name
{

    // field variable
    public int a, b;

      // member function or method
      public void display()
      {
          Console.WriteLine(“Class & Objects in C#”);
      }
}


Objects

It is a basic unit of Object-Oriented Programming and represents real-life entities. A typical C# program creates many objects, which as you know, interact by invoking methods. An object consists of :

  • State: It is represented by attributes of an object. It also reflects the properties of an object.

  • Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.

  • Identity: It gives a unique name to an object and enables one object to interact with other objects.

Consider Dog as an object and see the below diagram for its identity, state, and behavior.


Identity: Name the Dog

Attributes: Breed, Age, Color

Behaviour: Bark, Sleep, Eat


Objects correspond to things found in the real world. For example, a graphics program may have objects such as “circle”, “square”, “menu”. An online shopping system might have objects such as “shopping cart”, “customer”, and “product”.

Declaring Objects (Also called instantiating a class)

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

Example:

Class Dog

As we declare variables like (type name;). This notifies the compiler that we will use the name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, the type must be strictly a concrete class name.

Dog tuffy;

If we declare a reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.

Initializing an object

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.

Example:

using System;
 
public class Dog {
 
    String name;
    String breed;
    int age;
    String color;
     
    public Dog(String name, String breed,
                  int age, String color)
    {
        this.name = name;
        this.breed = breed;
        this.age = age;
        this.color = color;
    }
 
    public String GetName()
    {
        return name;
    }
 
    public String GetBreed()
    {
        return breed;
    }
 
    public int GetAge()
    {
        return age;
    }
 
    public String GetColor()
    {
        return color;
    }
 
    public String ToString()
    {
        return ("Hi my name is " + this.GetName()
                + ".\nMy breed, age and color are " + this.GetBreed()
                + ", " + this.GetAge() + ", " + this.GetColor());
    }
 
public static void Main(String[] args)
    {
         
        Dog tuffy = new Dog("tuffy", "papillon", 5, "white");
        Console.WriteLine(tuffy.ToString());
    }
}



Resource: Geeksofgeeks, Microsoft


The Tech Platform

0 comments
bottom of page