Defining a Class

A C# class let’s you define a user-defined data type that contains fields, properties, and methods. A class serves as a blueprint for an object. The  object is the actual thing that follows the structure or possesses the properties and behaviors of the class. When you create an object, you call them an instance          of a class. I may use the terms object and instance interchangeably.

At first, you may think that classes are similar to structures. The main difference between a class and a structure is that classes are reference types          and structures are value types. This will be demonstrated further in a later lesson.

We have been defining class since the beginning and that was the Program class. It contains the Main() method which is the starting point of the execution of the program. Defining a class is the same as defining a structure. But instead of using the struct keyword, we can tell the compiler that we are indicating a class by using the class keyword.

class ClassName
{
    field1;
    field2;
    ...
    fieldN;

    method1;
    method2;
    ...
    methodN;  
}

We write the keyword class followed by the class name. Class names also adhere to the Pascal Casing naming convention. Inside the body of the class, we provide the fields and methods it contain. Fields are the private data members that the class use for its behaviors and for storing the values of its properties. The methods are the behavior of the class or what it can do. Example 1 shows an example of defining and using a simple class named Person.

using System;
 
namespace ClassesDemo
{
    public class Person                                    
    {                                                      
        public string name;                                
        public int age;                                    
        public double height;                              
                                                           
        public void TellInformation()                      
        {                                                  
            Console.WriteLine("Name: {0}", name);          
            Console.WriteLine("Age: {0} years old", age);  
            Console.WriteLine("Height: {0}cm", height);    
        }                                                  
    }                                                      
 
    public class Program
    {
        public static void Main()
        {
            Person firstPerson = new Person(); 
            Person secondPerson = new Person();
 
            firstPerson.name = "Jack";         
            firstPerson.age = 21;              
            firstPerson.height = 160;          
            firstPerson.TellInformation();     
 
            Console.WriteLine(); //Separator
 
            secondPerson.name = "Mike";        
            secondPerson.age = 23;             
            secondPerson.height = 158;         
            secondPerson.TellInformation();    
        }
    }
}

Example 1 – Your First Class

Name: Jack
Age: 21 years old
Height: 160cm

Name: Mike
Age: 23 years old
Height: 158cm

The program above contains two classes, the Person class and the Program class. We know that the Program class contains the Main()method which is required in a program so let’s focus our attention on the Person  class.

Lines 5-17 is the declaration of our Person class.  Line 5 specifies the name of the class and its accessibility. We used the public access specifier so other classes can  recognize the Person class. We’ll take a look at access specifiers in a separate lesson. Inside the body of the class are fields for that class  (lines 7-9). We defined three fields that resemble the real properties of a real-world person. A person has a name, age, and height.  Lines 11-16 defined one method inside the class named TellInformation(). It is the behavior of our class as if we are asking the  Person object to tell something about himself. Inside the method are codes that will display the currently stored values of the fields.  You can see that it simply shows the values of the class’ fields. One thing to  note about fields. Since they are declared inside the class and considered as  class members, they have a class scope. It means that these fields can only be  used inside the class where it belongs or by an instance of a class.

Inside the Main() method, lines 23-24 created two instances or objects of Person class. When declaring an instance of a class, we use the new keyword followed by the name of the class and a set of parentheses. When creating an instance of a class, you need to call the constructor. A constructor is a special method used to initialize values of the fields of an object. The class calls the default parameterless constructor when you don’t specify arguments inside the parentheses. We’ll take a look at constructors in the next lesson.

Lines 26-28 assigns values to the fields of the first Person object. We used the dot syntax when accessing fields or methods of an object.  For example, calling firstPerson.name refers to  the name field of the firstPerson. We called the TellInformation()method of the firstPerson to print the values of its  fields. Lines 33-36 does the same to the secondPerson object. We also assigned values to it and call the TellInformation() method. Note that firstPerson and secondPerson has different copies of each field so specifying a name for the secondPerson does not affect the name of the firstPerson. You will learn about class members that are shared by all the instances of the class in a later lesson.