What is Class ? Explain it in C#

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.

Class Creation in C#

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.

Image is not available

Points to Remember

  • Point 1: A Class is Reference Type in C#.
  • Point 2: When we create a Class, By-default Class is internal.
  • Point 3: All Classes has a Base type that is System.Object in C#.
  • Point 4: We can use Public,Private,internal,Protected and Protected internal access modifier with a Class.
  • Point 4: If there is no any access specifier then bedefualt members of Class is Private.