C Sharp Object and Class

C# Object and Class

Program in C# uses objects and classes as it is an object-oriented programming language.

C# Object:

An entity with a state and behavior can be called as an Object. In terms of an object-oriented programming language, the state of an object implies data and the term behavior implies functionality. An object is created at runtime and is thus a runtime entity. Any member of a class can be accessed through an object, as it is used as an instance of a class.

Example:

Employee e1 = new Employee();

Explanation:

In the above example, we are creating an object using the new keyword to allocate memory at runtime. Here, Employee is the type and e1 is the reference variable. The reference variable refers to the instance of the Employee class.

C# Class:

Being a template from which objects are created, Class in C# is a group of similar objects. Fields, methods, constructors, etc. can also be included in a C# class.

Example:

public class Employee 
{  
int id;
String city;
}  

Explanation:

In the above example, we are creating a C# class with two fields.

Example 1:

using System;  
  public class Employee  
    {  
        int emp_id;  
        String emp_name;   
 
    public static void Main(string[] args)  
        {  
            Employee e1 = new Employee();//creating an object of Student    
            e1.emp_id = 10;  
            e1.emp_name = "Vishal Gupta";  
            Console.WriteLine(e1.emp_id);  
            Console.WriteLine(e1.emp_name);  
        }  
    }

Output:

Explanation:

In the above example, we are creating a class with two fields: emp_id and emp_name, thus creating an instance of the class, initializing the object and printing the object value.

Example 2:

using System;  
   public class Employee  
    {  
        public int emp_id;   
        public String emp_name;  
   }  
   class Details{  
       public static void Main(string[] args)  
        {  
            Employee e1 = new Employee();    
            e1.emp_id = 10;  
            e1.emp_name = "Vishal Gupta";  
            Console.WriteLine(e1.emp_id);  
            Console.WriteLine(e1.emp_name);  
 
        }  
    }

Output:

Explanation:

In the above example, we are creating the Main() method in another class, thus the class must be public.

Example 3:

using System;  
   public class Employee  
    {  
        public int emp_id;   
        public String emp_name;  
        public void insert(int x, String y)  
        {  
            emp_id = x;  
            emp_name = y;  
        }  
        public void display()  
        {  
            Console.WriteLine(emp_id + " " + emp_name);  
        }  
   }  
   class Details{  
       public static void Main(string[] args)  
        {  
            Employee e1 = new Employee();  
            Employee e2 = new Employee();  
            e1.insert(10, "Vishal Gupta");  
            e2.insert(11, "Thomas Edison");  
            e1.display();  
            e2.display();  
 
        }  
    }

Output:

Explanation:

In the above example, we are creating a class in C#. Here, we are initializing and displaying objects through the method.

Example 4:

using System;  
   public class Cities  
    {  
        public int id;   
        public String city_name;  
        public void insert(int x, String y)  
        {  
            id = x;  
            city_name = y;  
        }  
        public void display()  
        {  
            Console.WriteLine(id + " " + city_name);  
        }  
   }  
   class Details{  
       public static void Main(string[] args)  
        {  
            Cities c1 = new Cities();  
            Cities c2 = new Cities();  
            c1.insert(10, "Vishal Gupta");  
            c2.insert(11, "Thomas Edison");  
            c1.display();  
            c2.display();  
 
        }  
    }

Output:

Explanation:

In the above example, we are storing and displaying the Cities information.

Please Share