C-SHARP - 3.3 Class (demo)
We are now in Visual Studio to create a class. We start with the access modifier, here public, so the class is reachable everywhere in the application. We will explore all access modifiers in detail later, but for now remember: use public when you want the class to be available from any project file. After the access modifier we add the class keyword, give the class a name (for example Voiture) and open braces. Inside, we declare the attributes that describe the car: brand, year and colour. Each attribute also has an access modifier, a type and a name, and ends with a semicolon.
public class Voiture
{
public string Marque;
public int Annee;
public string Couleur;
public void AfficherVoiture()
{
Console.WriteLine("Marque : " + Marque + " Annee : " + Annee + " Couleur : " + Couleur);
}
}
A class can also contain methods. We add a public void AfficherVoiture() method that displays the characteristics of the car using Console.WriteLine. void means the method returns nothing, and empty parentheses indicate it takes no parameter. To create an object, we go back to the Main method and declare a variable whose type is the class name, then use new to allocate memory and call the constructor. We can then assign values to the attributes using the dot notation and call the method.
Instantiating and using the class
Voiture voiture = new Voiture();creates an instance.voiture.Marque = "Peugeot";assigns an attribute through dot notation.voiture.AfficherVoiture();calls the method on the instance.
Keeping every class inside Program.cs would quickly bloat the file and slow the project down. Visual Studio offers a quick refactoring: click on public class Voiture, then on the light bulb icon and choose Move to Voiture.cs. The class lands in its own file but remains usable because it is declared public. You can also right-click the project, choose Add > Class, give it a name (for example Moto) and Visual Studio generates the file for you.
Summary
This practical lesson demonstrates how to create a class in C# using Visual Studio, walking through the complete process from declaring a class with access modifiers to defining attributes and methods. Students learn to instantiate objects using the `new` keyword, access class members via dot notation, and assign values to attributes. The lesson concludes with best practices for code organization by moving classes to separate files rather than keeping everything in the main program file.
Key points
- Declare a class using the public keyword followed by the class keyword and a descriptive class name
- Define class attributes with appropriate access modifiers and data types (e.g., public string for brand, public int for year)
- Create methods with specified return types and parameter lists to define class behavior
- Instantiate objects using the new keyword to allocate memory and create instances
- Access attributes and methods using dot notation (e.g., voiture.marque = "Peugeot")
- Maintain clean code architecture by placing each class in its own separate file
FAQ
What is the purpose of the `new` keyword when creating an object?
The `new` keyword allocates memory for the object instance. Without it, the variable would reference the class definition itself rather than creating an actual instance of the class that you can work with.
Why should I use the `public` access modifier for my class and attributes?
The `public` access modifier makes your class and its members accessible from anywhere in your application, allowing other parts of your program to use and interact with the class and its attributes.
What are the advantages of placing each class in a separate file?
Separating classes into individual files keeps your code organized and maintainable, prevents the main program file from becoming bloated, improves application performance, and helps avoid excessive memory consumption.