Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Classes and Object in C#

C# Object and Class

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

C# Object

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

In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality.

Object is a runtime entity, it is created at runtime.

Object is an instance of a class. All the members of the class can be accessed through object.

Let’s see an example to create object using new keyword.

Student s1 = new Student();//creating an object of Student   

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

C# Class

In C#, class is a group of similar objects. It is a template from which objects are created. It can have fields, methods, constructors etc.

Let’s see an example of C# class that has two fields only.

   
public class Student   {  
         int id;//field or data member   
         String name;//field or data member  
 }  

C# Object and Class Example

Let’s see an example of class that has two fields: id and name. It creates instance of the class, initializes the object and prints the object value.

   
using System;  
public class Student  {  
    int id;//data member (also instance variable)    
    String name;//data member(also instance variable)   
    public static void Main(string[] args)  {  
        Student s1 = new Student();//creating an object of Student    
        s1.id = 101;  //using the dot operator to access members of a class
        Console.WriteLine(s1.id);  
        Console.WriteLine(s1.name);  

    }  
}    

Output:

101
Sonoo Jaiswal

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

Let’s see another example of class where we are having Main() method in another class. In such case, class must be public.

     
using System;  
public class Student  {  
    public int id;   
    public String name;  
}  
class TestStudent{  
    public static void Main(string[] args)  {  
        Student s1 = new Student();  //creating an object of Student   
        s1.id = 101;  //using the dot operator to access members of a class
        s1.name = "Sonoo Jaiswal";  //using the dot operator to access members of a class
        Console.WriteLine(s1.id);  
        Console.WriteLine(s1.name);  

    }  
}  

Output:

101
Sonoo Jaiswal

C# Class Example 3: Initialize and Display data through method

Let’s see another example of C# class where we are initializing and displaying object through method.

using System;  
public class Student  {  
	public int id;   
	public String name;  
	public void insert(int i, String n) {  
		id = i;  
		name = n;  
	}  
	public void display() {  
		Console.WriteLine(id + " " + name);  
	}  
}  
class TestStudent{  
	public static void Main(string[] args)  {  
		Student s1 = new Student();  
		Student s2 = new Student();  
		s1.insert(101, "Ajeet");  
		s2.insert(102, "Tom");  
		s1.display();  
		s2.display();  
      
	}  
}  

Output:

101 Ajeet
102 Tom

Try the following example in the editor below.

We have a class defined for Student. Create a function insert which takes three parameters name(string), age(int), rollno(int) and assign values to the member variables. Also, create a function display which prints name, age, rollno each in new line.

Start solving Classes and Object in C# on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test