C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It can be anything that exists in the real world and that has state and behavior for eg. Car, pen, chair, etc. In C#, object is a real time entity. It is an instance of the class which can access all the fields and methods of the class. Let's see an example of creating object of a class using new keyword. Student s=new student(); // creating object of student class Example illustrates the creation of object of student class. s is the reference variable to the object for which the memory is provided at runtime by new keyword. C# Class: In C#, class is the group of similar type of objects. A class can have static and non static methods. It can have fields and constructors. It represents the template of its object.

Let's see an example of C# class.
public class student
{
int id; //integer field
string name; //string field
}
C# Object and class Example:
Let's see an example of class and object. The employee class has two fields named as id and name. the fields are initialized through object emp created in main method which is in same class.
using System;
public class Employee
{
            int id;
            string name;
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.id=100;
            emp.name="Ram Sharma";
            Console.WriteLine(emp.name+" "+emp.id);
}
}
Output
Ram Sharma 100        
C# Class and object Example: Accessing class data from another class
We can also create object of the class in another class but the class fields and methods must be public.
using System;
public class Employee
{
            public int id;
            public string name;
            public void display()
            {
                        Console.WriteLine(id+" " +name);
            }
}
public class MainClass{
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.id=100;
            emp.name="Ram Sharma";
            emp.display();
}
}
Output
100 Ram Sharma
C# Example: printing the class variables through method
using System;
public class Employee
{
            int id;
            string name;
            void display()
            {
                        Console.WriteLine(id+" " +name);
            }
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.id=100;
            emp.name="Ram Sharma";
            emp.display();
}
}
Output
100 Ram Sharma        
C# classes and objects Example : Initializing class data by a method
Till now, we were initializing class fields in the main method. We can also create a method to initialize class data as shown below.
using System;
public class Employee
{
            public int id;
            public string name;
            public void setValues(int emp_id,string emp_name)
            {
                        id=emp_id;
                        name=emp_name;
            }
            public void display()
            {
                        Console.WriteLine(id+" " +name);
            }
}
public class MainClass{
public static void Main (string[] args)
{
            Employee emp=new Employee();
            emp.setValues(200,"Harish Gupta");
            emp.display();
}
}
Output
200 Harish Gupta
C# Class and objects Examples: Storing and Retrieving information of various students
In the following Example, We are storing the student’s information through method and retrieving it.
using System;
public class Student 
{
            int roll_no;
            string name,course,batch;
public void setValues(int student_roll_no,string student_name,string student_course,string student_batch)
            {
                        roll_no=student_roll_no;
                        name=student_name;
                        course=student_course;
                        batch=student_batch;                      
            }
            public void display()
            {
                        Console.WriteLine(roll_no+" " +name+" "+course+" "+batch);
            }
}
public class MainClass{
public static void Main (string[] args)
{
            Student s1=new Student();
            s1.setValues(101,"Harish Gupta","B. Tech.","2016");
            Student s2=new Student();
            s2.setValues(102,"Ayush Sharma","B. Tech.","2017");
            s1.display();
            s2.display();
}
}
Output:
101 Harish Gupta B. Tech. 2016
102 Ayush Sharma B. Tech. 2017



ADVERTISEMENT
ADVERTISEMENT