find and solve || findandsolve.com
Please wait.....

C# Classes and Objects

In the C#, Everything is associated with classes and objects which is dependent on basic concepts of Object-Oriented Programming.

C# is an object-oriented language, program is designed using objects and classes in C# programming language.

C# Object

In C#, Object is a real-world entity, for example, real, dog, car, pen, mobile, laptop etc.

In other words, object is the new instance of class or an entity that has a state and behavior. All the member's values are  accessed through object.

Let's see in the given below example to create object using new keyword.

Employee objEnployee= new Employee ();//creating an object of Employee 

In this example, Employee is the type and objEmployee is the reference variable that refers to the instance of Employee class. The new keyword allocates memory at runtime.

C# Class

In C#, class is a collection of properties or group of similar objects.

To create a class, use the class keyword like as given below.

public class Employee
 {
     public int ID;//field or data member
     public string FullName;//field or data member
 }  

C# Class and Object Example

An object is created from a class. I have already create the class named Employee, so now you can use this to create objects.

To create an object of Employee, specify the class name, followed by the object name, and use the keyword new:

   using System;  
   public class Employee
    {
        public int ID;//data member (also instance variable)
        public string FullName;//data member(also instance variable)
    public static void Main(string[] args)
        {
            Employee objEmployee= new Employee();//creating an object of Employee
            objEmployee.ID= 1002;
            objEmployee.FullName= "Sylvia Neupane";
            Console.WriteLine(objEmployee.ID);
            Console.WriteLine(objEmployee.FullName);
        }
    } 

Output

1002

Sylvia Neupane


Multiple Objects


   using System;
   public class Employee
    {
        public int ID;//data member (also instance variable)
        public string FullName;//data member(also instance variable)
       public static void Main(string[] args)
        {
            Employee objEmployee1= new Employee();//creating an object of Employee: first object
            Employee objEmployee2= new Employee();//creating an object of Employee: second object
            objEmployee1.ID= 1002;
            objEmployee1.FullName= "Sylvia Neupane";
            Console.WriteLine(objEmployee1.ID);
            Console.WriteLine(objEmployee1.FullName);
            objEmployee2.ID= 1003;
            objEmployee2.FullName= "Rabina Neupane";
            Console.WriteLine(objEmployee2.ID);
            Console.WriteLine(objEmployee2.FullName);      
    }
  } 

Output

1002

Sylvia Neupane

1003

Rabina Neupane

Another C# Class Example: Having Main() in another class

   using System;  
   public class Employee
    {         public int ID;
        public strinig FullName;
   }
   public class EmployeeInfo{
       public static void Main(string[] args)
        {
            Employee objEmployee= new Employee();
            objEmployee.ID= 1002;
            objEmployee.FullName= "Sylvia Neupane";
            Console.WriteLine(objEmployee.ID);
            Console.WriteLine(objEmployee.FullName);
        }
    }  

Output

1002

Sylvia Neupane


C# Class : Initialize and Display data through method

    using System;  
    public class Employee
    {
        public int ID;
        public string FullName;
        public void insert(int i, String n)
        {
            Id= i;
            FullName= n;
        }
        public void display()
        {
            Console.WriteLine(ID+ " " + FullName);
        }
   }
   public class EmployeeInfo{
       public static void Main(string[] args)
        {
            Employee objEmployee= new Employee ();
            Empoyee objeEmployee2= new Employee();
            objEmployee.insert(1002, "Sylvia Neupane");
            objEmployee2.insert(1003, "Rahul Neupane");
            objEmployee.display();
            objEmployee2.display();
        }
    }  

Output

1002 Sylvia Neupane

1003 Rahul Neupane

C# Class : Store and Display Employee Information

   using System;  
   public class Employee
    {
        public int id;
        public String name;
        public float salary;
        public void insert(int i, String n,float s)         {
            id = i;
            name = n;
            salary = s;
        }
        public void display()
        {
            Console.WriteLine(id + " " + name+" "+salary);
        }    }   public class EmployeeInfo{
       public static void Main(string[] args)
        {
            Employee objEmployee= new Employee();
            Employee objEmployee2= new Employee();
            objEmployee.insert(1002, "Sylvia",450000f);
            objEmployee2.insert(1003, "Rahul", 510000f);
            objEmployee.display();
            objEmployee2.display();
        }
    }  

Output

1002 Sylvia 450000

1003 Rahul 510000