Les classes en C#

julioz, kolowy | Oct 14, 2023 min read

Written for a C# class

What is a class? A class is like an object. In this example, we will consider a class Person. We can add attributes to the class. Attributes are sort of variables that are defined in the class and belong to the object they are defined in. For example, for a class Person, we can create the attributes name and age, that will contain the person’s information.

Let’s create a house class as an example:

class Person
{
    private string name; // a attribute, for the person's name
    private int age; // another attribute, for the person's age
}

We might want to access to those attributes, using the keyword public. A getter is a way to look at the value of the private variable from outside its class. A setter is like a way to change the value of the private variable from outside the class. They are called properties of your attributes. You can create them this way:

class Person
{
    private string _name;
    private int _age;

    public string Name // property for the name
    {
        get { return _name; } // getter
        set { _name = value; } // setter
    }

    public int Age // property for the age
    {
        get { return _age; } // getter
        set { _age = value; } // and a setter
    }
}

You can also do it in a shorter and easier way:

class Person
{
    public string Name { get; set; } // getter and setter for the attribute Name
    public int Age { get; set; } // getter and setter for the attribute Age
}

Now, we want to do some stuff with our class, and to do so, we will use methods. They defind the actions the class can do.

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void HeyYou() // a method
    {
        Console.WriteLine($"Hey, I am {Name}, I'm {Age} years old!");
    }

    public void CheckTime(string time) // another method
    {
    Console.WriteLine($"What time is it, {Name}?");
    if (time == "Friday")
        Console.WriteLine("Check my time, it's Friday");
    else
        Console.WriteLine("I don't know");
    }
}

A constructor is a method that is called when you create a new object of your class. It is used to set the initial state of the object. This method is a bit special, because it has no return type and MUST have the exact same name as the class. It’s even more special because it won’t be called like other methods, but we’ll see that a bit later.

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age) //the constructor
    {
        this.Name = name;
        this.Age = age;
    }

    public void HeyYou()
    {
        Console.WriteLine($"Hey, I am {Name}, I'm {Age} years old!");
    }

    public void CheckTime(string time)
    {
        Console.WriteLine($"What time is it, {Name}?");
        if (time == "Friday")
            Console.WriteLine("$Check my time, it's Friday");
        else
            Console.WriteLine("I don't know");
    }
}

Now, you can create your first Person, called an instance. An instance is a specific object created from the class. Here, your instance is stored in the variable smurf. Each instance has its own attributes name and age, and holds the methods of the class Person:

Person smurf = new Person("Smurf", 20);
Person smourf = new Person("Smourf", 15);
smurf.HeyYou();             // Hello, I'm Smurf, I'm 20 years old!
smourf.HeyYou();            // Hello, I'm Smourf, I'm 15 years old!

smourf.Name = "Smorf";
smurf.Age = 21;
smurf.HeyYou();             // Hello, I'm Smurf, I'm 21 years old!
smourf.HeyYou();            // Hello, I'm Smorf, I'm 15 years old!

smurf.CheckTime("Friday");  // What time is it, Smurf ?
                            // Check my time, it's Friday

Public or private?

We have seen what a public set is. Now, we will introduce a new keyword: private. First of all, let's talk about the difference between a public member and a private one. The public members are accessible from both inside and outside of the class they are defined in. The private members are only accessible from within the class they are defined in. It is the default access of a member. It is the same thing for sets. In comparaison to a public set, a private set adds restriction to the modification of the property. Indeed, the private keyword will block all the extern modification, therefore you can only modify the property in a method of the same class. This way, other external classes cannot directly modify its value.
class Person
{
    public string Name { get; private set; } // getter and private setter for the attribute Name
    public int Age { get; set; } // getter and setter for the attribute Age
}

class Program
{
    public static void Main()
    {
        Person person = new Person();
        person.Name = "Test."; // This line will produce an error. In the class Program, we can't modify the Name of the instance
        person.Age = 12; // But we could modify the Age of the instance
    }
}