Classes and objects are the core of concepts of OOPs methodology.  In OOPs system objects are the building blocks of the development process and they represent real world things.

Objects take place in memory while class is the logical abstraction. They are instances of the class. A class is the blueprint of an object which defines the data and operations for objects. Classes act as a built-in data type in programming languages.

Defining Class:

 

Class and Objects in C#Image Source: c-sharpcorner.com

In C# class is defined with keyword ‘class’ and the body of the class is enclosed in curly braces.

The general syntax for defining a class is:

 

class <ClassName> 
{ 
    //Fields declaration 
    //Method definitions 
}

 

Let’s create class Person as: 

 

class Person 
{ 
    //Fields 
    public string Name; 
    public string Address; 
   //Methods 
     public void Display() 
     {
         Console.WriteLine("Name of the Person is: {0}",Name);
         Console.WriteLine("Address is: {0}", Address); 
     } 
}

 

Fields: 

Fields represent the data in class. They may be variables of built-in data types, objects of another class, enums, structs or delegates. They are also called as instance variables or data members.

Each object has its own copy of field’s means that the data of every object are kept separate. In above example Name and Address are the fields in class person.

Methods:

The operations to be performed on data of a class are defined by methods. The method takes zero or more inputs through parameters and may return a single value or does not return anything. They are also called as member functions.

The general syntax of the method is as below:

 

<return type><method name>(parameter list) 
{ 
    // Operations to be done. 
}

 

In above example of class Person, the Display() method is defined to show data of the person.

Recommended Post   C# Data Types & Variables

Object Creation: 

The objects of the class are created using the new operator. New operator creates the object of the class and allocates memory to that object.

The object created as below:

 

Type <obj_name> =new Type();

 

The object of Person class is created as below:

 

Person P=new Person();

 

Or it can be done in two statements as:

 

Person P; 
P=new Person();

 

The important thing about above two lines is that in the first line declare only reference variable ‘P’ but does not create any object at this point.

So if anybody tries to access class members at this point, it will throw compile time error. In the second line of the above code object of Person class is created and the reference to that object is assigned to reference variable P.

 

Accessing members: 

To access the members of class dot operator (.) is used. Once an object of the class is created, we can set and get the value of fields with the dot operator and object. Also, methods are also accessed using dot operator.

The members of class Person are accessed as below:

 

Person P=new  Person(); 
P.Name=”Ganesh”; 
P.Address=”Mumbai”; 
P.Display();

 

Let’s put all this together in below program:

 

using System; 
namespace ClassDefinition 
{ 
     class Person 
    { 
       //Fields 
       public string Name; 
       public string Address; 
       //Methods 
       public void Display() 
       { 
           Console.WriteLine("Name of the Person is:{0}", Name); 
           Console.WriteLine("Address:{0}", Address); 
       } 
   } 
   class Program
   { 
      static void Main(string[] args) 
      { 
           Person P1 = new Person();    //Object Creation 
           P1.Name = "Ganesh";          //Accessing fields 
           P1.Address = "Mumbai";
           P1.Display();               //Accessing methods 
           Console.ReadLine(); 
      }
   } 
}

 

Recommended Post   C# Interface

The output of above program is:

 

Name of the Person is: Ganesh 
Address is: Mumbai

 

Static Members: 

The members (fields and methods) we have seen so far are the instance members of the class, which means they are related to their objects. Each object has its own copy of instance members. There is another one type of members called ‘Static Members.

Only one copy of static members is created and shared by all objects of class no matter how many objects are created. In C# static keyword is used to declare static members.

To access static member we need not create any object of the class. Static members are accessed using class.

We can declare both fields and methods as static as below.

 

Static Variables: 

If the fields (data members) of a class are declared with the static keyword then they are known as static variables. Only a single copy of static variables is created and this copy is shared by all objects of the class.

Following program shows the difference between instance variables and static variables:

 

using System; 
namespace StaticMembers 
{
    class Program
    { 
        class StaticDemo 
        { 
            public static int staticNumber=10;   
            public int number=100; 
            public void Display() 
            { 
                Console.WriteLine("The value of variable number is {0}",number);
                Console.WriteLine("The value of variable staticNumber is {0}", staticNumber); 
            } 
        } 
        static void Main(string[] args) 
        {
            StaticDemo Demo1 = new StaticDemo(); 
            StaticDemo Demo2 = new StaticDemo(); 
            Demo1.Display(); 
            Demo1.number++;                //Accessing instance variable 
            StaticDemo.staticNumber++;  //Accessing static variable 
            Demo1.Display();
            Demo2.Display(); 
            Console.ReadLine(); 
       } 
    } 
}

 

Recommended Post   C# Type Conversion

The output of above program is:

 

The value of variable number is 100 
The value of variable staticNumber is 10 
The value of variable number is 101 
The value of variable staticNumber is 11 
The value of variable number is 100 
The value of variable staticNumber is 11

 

Static Methods:

If a method declared with static keyword, then it is known as a static method. Like static variable to access static methods, there is no need to create an object of the class, these methods are accessed using the class name and dot operator. Inside static methods, instance variables are not accessible.

 

using System; 
namespace StaticMembers 
{ 
    class Program 
    {
        class StaticDemo 
       { 
           public static int staticNumber = 10; 
           public int number = 100; 
           static public void Display() 
           { 
               //Console.WriteLine("The value of variable number is {0}", number); //Instance varibles not accessible here 
                Console.WriteLine("The value of variable staticNumber is {0}", staticNumber); 
           }
      }
      static void Main(string[] args) 
      { 
           StaticDemo.Display();   //Accessing Static method 
           Console.ReadLine();
      }
   }
}

 

The output of above program is: 

 

The value of variable staticNumber is 10
SHARE
Previous articleC# OOPS
Next articleC# Methods 

LEAVE A REPLY

Please enter your comment!
Please enter your name here