OOPS Concept in C#

Object oriented programming, or the OOPS concept in C#, is a type of programming that contains a 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 (OOPS concept) 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 for implementing 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 in the oops concept is said to be the instance of a class, and the Object holds data fields and 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 Object has some properties for identifying its current state and for the sake of validation.
  • 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 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 surrounded by blades, its brand, and its 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 class, an object is created for the class.

C# Class Instance syntax in OOPS concept

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

new is the keyword used here 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. Seeing an example of each one will help you find the differences among them.

C# Variable

The C# Variable in oops concept is nothing but a copy of a class that 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, the data type int and the class string is a blueprint with no memory allocated to it. But for the variables x and s, memory is allocated.

So a 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 is allocated.

Say, for example, a house plan is just like the datatype, and the physical house constructed based on that plan is 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

object oriented Programming or 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 an error. And the 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

object oriented Programming OOPS Concept in C# 2

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

C# Instance

The C# instance in the OOPS concept is nothing but a copy of the class, which helps initialize a variable using the 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 are created; the memory allocations are 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# Object oriented programming OOPS concept class 3

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

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

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

C# Reference

The Reference in the C# oops concept is also a copy of the class, which initializes along with an existing instance. Still, a reference doesn’t have any memory allocated. Instead, it is 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 reflect the instance to which it was 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

object or instance 4

In the C# OOPS concept 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.

Categories C#