OOPS Concept in C#

Object oriented programming or OOPS concept in C# is a type of programming that contains the collection of objects. Each Object contains data fields and data members. Unlike procedural programming languages like C, FORTRAN, BASIC, etc., C# Object oriented programming languages can easily upgrade.

Here, we shall learn the concepts of C# OOPS (Object oriented programming) like Class, Object, Encapsulation, inheritance, polymorphism, etc. Here, encapsulation, inheritance, and polymorphism are the mechanism that is useful to implement the C# Object oriented programming model.

C# Class

A class is a user-defined type, and it is the main concept of the C# OOPS or object oriented programming.

A Class is just like a blueprint or a template for creating an object. The C# class doesn’t have any memory allocated. But the memory is allocated only for the created class instance. The syntax of the C# class is

class <classname>
{

}

C# Object

The C# Object is said to be the instance of a class, and Object holds data fields, member functions(data members).

Actually, in a real-time environment, everything is said to be an object. The same is applicable in this C# object oriented programming.

  • An object is an entity that can be seen or imagined.
  • The C# Object has some properties for identifying its current state and for the sake of validations.
  • Data members or member functions for telling their functions or behavior.
  • Events for representing the change of its state.

So we can say that the C# objects can distinguish from one another based on their state, functionality, and event. For example, if we consider a revolving fan as an object, 

  • A nob surrounding by blades, its brand, the color of it depicts the state of the revolving fan.
  • The airflow created by the fan is said to be its functionality.
  • Whereas change in the position of blades while revolving represents the change of its state.

In C# object oriented terminology, we say that to access the data fields and the member functions inside a C# class, an object is created to the class.

C# Class Instance syntax

<ClassName> <ObjectName> = new <ConstructorName(<parameter list>)

Here, new is the keyword used while creating an object for a C# class.

Differences among C# Variable, Instance (Object) and Reference

This section covers the C# Variable, Instance (Object), and Reference. By seeing an example of each one, will help you find the differences among them.

C# Variable

The C# Variable is nothing but a copy of a class which is not initialized, for example.

int x; x is a copy of the type int.

string s; String is a class, and s is the copy of the class string.

Here data type int and the class string is a blueprint that doesn’t have any memory allocated to it. But for the variables x and s, memory is allocated.

So a C# variable is a copy of a particular data type that holds some memory. In contrast, the C# datatype is a logical one or a blueprint for which no memory allocated.

Say, for example, a house plan is just like the datatype, and the physical house constructed based on that plan is the variable. i.e., int or string in the above context doesn’t have any memory whereas a variable has physical memory.

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharp_Tutorial
{
    class Class1
    {
        int x = 100;
        static void Main(string[] args)
        {
            Console.WriteLine(x);
        }
    }
}

OUTPUT

OOPS Concept in C# 1

In the above C# OOPS program, when we try to print the variable x in the main method, it shows error. And the C# error says that you are trying to print the non-static class variable in the static main method.

In C# object oriented programming, we have to create an instance for that class to initialize variables or print any value in the main method. And use that instance to print the values, or else it throws an error, as shown in the following code.

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharp_Tutorial
{
    class Class1
    {
        int x = 100;
        static void Main(string[] args)
        {
            Class1 c = new Class1();
            Console.WriteLine(c.x);
        }
    }
}

OUTPUT

OOPS Concept in C# 2

The reason for this is a C# variable in object oriented programming is a copy of the class that not initialized.

C# Instance

The C# instance in OOPS concept is nothing but a copy of the class, which helps initialize a variable using a keyword new. Every instance has its own memory. 

The memory allocated for one C# instance never shares with another instance. It means how many instances created; the memory allocations allocated for that many instances without memory sharing.

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharp_Tutorial
{
    class Class1
    {
        int x = 50;
        public Class1()
        {
            Console.WriteLine("value of x is " + x);
        }
        public Class1(int i)
        {
            Console.WriteLine("value of i is " + i);
        }


        static void Main(string[] args)
        {
            Console.WriteLine("Default Constructor is invoked");
            Class1 c = new Class1();
            Class1 c1 = new Class1();
            Console.WriteLine("Parameterized Constructor is invoked");
            Class1 c2 = new Class1(100);
            Class1 c3 = new Class1(200);


        }
    }
}

OUTPUT

C# class 3

In the above C# oops program, four instances have created for Class1,

Instance c, c1 created using default or parameterless constructor Class1(). Whereas instance c2, c3 are created using parameterized constructor Class1(int i).

All instances that created irrespective of the constructor used will have allocated separate memory locations.

C# Reference

The Reference in C# is also a copy of the class, which initializes along with an instance that already existed. Still, a reference doesn’t have any memory allocated. Instead, it created for sharing the memory of the instance.

A reference to a C# class can also say as a pointer to the instance. Every action performed on the fields and member functions using the instance a reference pointed to reflects that Reference. And vice versa (changes made using Reference reflects the instance to which it created).

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharp_Tutorial
{
    class Class1
    {
        int x;
        public Class1(int i)
        {
            x = i;
        }


        static void Main(string[] args)
        {
            Class1 c = new Class1(100);
            Class1 c2 = c;
            Console.WriteLine("x value using instance c");
            Console.WriteLine(c.x);
            Console.WriteLine("x value using reference c2");
            Console.WriteLine(c2.x);
        }
    }
}

OUTPUT

C# object or instance 4

In the C# OOPS code written above, c is the instance of Class1, whereas c2 is the Reference created to c.

When we try to print the value of x using instance c and reference c2 is the same. The output, i.e., 100 printed also using reference c2, is because it pointed to the instance c.