Object is a way by which we can access the members and member functions of any Class. In short, we can say that Object is the representation of any Class , by which we can represent real world entities. An Object is an instance of a Class and it has a block of memory to hold values of class members.Objects are the building blocks of any Object Oriented Programming language like C# and every Object has a Identity, every Object has a State and behaviour that uniquely define a Object from other Objects of a Class. We can create multiple Object of same Class.
We can create an Object of any Class using the new keyword and we can initialize the members of any Class using the Constructor. When we create an Object of any Class and pass the values in Constructor parameter then it will hold the space in memory for Object.
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }=string.Empty;
Public int Age { get; set; }
Public bool Gender { get; set; }
public string StudentMobile { get; set; }=string.Empty;
public Student(int studentID, string studentName, int age, string gender, string studentMobile)
{
StudentID = studentID;
StudentName = studentName;
Age = age;
Gender = gender;
StudentMobile = studentMobile;
}
public void PrintStudentName(string name)
{
Console.WriteLine($"Student Name is - {name}");
}
}
In the above example I have created a public class and named it Student and inside this class I have declared five public properties that is StudentID, StudentName, Age, Gender, StudentMobile and one is public method PrintStudentName. This properties are initialized using the public constructor as shown in above code.
As we know that we can access the members and member functions of any class using the Object of that Class only , here in below image you can see I have created one Object of Stdudent Class using the new keyword and named it student. Using this Object we pass the values in construtor so that all the properties get the values from here or we can say that Object creation will initialized the Class members and Object will occupy the space in memory.
In the below image you can get the output of above code.