In C#, class and objects are interlinked, class can be defined as a collection of various data members like fields, properties, methods etc. While Object is an instance of a class used to access class method, fields etc.
We can consider class as a template, declaration or blueprint that is used for classifying the object. It contains variable members, functions, structure, properties and many more components. It is the basic building block of object-oriented programming. In order to create a class, the class keyword is used.
Syntax to declare a class
<access_specifier> class <class_name>
{
//class definition
}
Where access_specifier can be
Example of a C# class
using System;
public class Student
{
//class variable
public string name {get;set;}
public int age {get;set;}
//class methods
public void PrintName()
{
Console.WriteLine("Student name: "+name);
Console.WriteLine("Student age: "+age);
}
}
take a look at the complete example
using System;
public class Student
{
//class variable
public string name {get;set;}
public int age {get;set;}
//class methods
public void PrintName()
{
Console.WriteLine("Student name: "+name);
Console.WriteLine("Student age: "+age);
}
}
public class Program
{
public static void Main()
{
//create Student class object (student)
Student student = new Student();
//assign value to student class variables, using object
student.age=15;
student.name="John";
//call student class method using object.
student.PrintName();
}
}
Output:
Student name: John
Student age: 15
As you can see in the above code, we have create a Class named as "Student
" and then added data members like property Age
, Name
.
Inside the main program, we have created class object "student
" and then assigned value of name,age using it. We have also called Student class method "PrintName" using student
object.
While creating class remember these points
As you can see in the above example, object is an instance of a class, we create object to access methods, variable etc of class.
Syntax to create object
class_name object_name = new class_name();
Example
Student studentobj= new Student();
If you observe above example, we created an instance (studentobj) for the class (Student) which we created in previous section. Now the instance "studentobj" is a reference to an object that is based on Student. By using object name "studentobj" we can access all the data members and member functions of Student class.
Example
using System;
public class Student
{
//class variable
public string name {get;set;}
public int age {get;set;}
//class methods
public void Print()
{
Console.WriteLine("Student name: "+name);
Console.WriteLine("Student age: "+age);
}
}
public class Program
{
public static void Main()
{
//create Student class object (studentobj)
Student studentobj = new Student();
//assign value to student class variables, using object
studentobj.age=15;
studentobj.name="Vikas";
//call student class method using object.
studentobj.Print();
}
}
Output:
Student name: Vikas
Student age: 15