Class is a User defined type and defined using Class Keyword. In simple words we can say that Class is a collection of data members and member functions. Data members includes fields,properties,events,indexers and member functions includes methods and constructors.
We can create a Class using Class keyword and we can define fields,properties and methods inside the class based on our requirements. Classes encapsulates all the data members (fields and properties) and member functions (methods,events) into a single unit.
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.