Classes in C#: A Comprehensive Guide

Category > CSHARP || Published on : Friday, March 3, 2023 || Views: 251 || declare a class create objects access properties and methods constructors inheritance access modifiers OOP


Classes are a fundamental concept in object-oriented programming (OOP), and they provide a powerful mechanism for organizing and structuring code. C# is an object-oriented language that provides a robust set of features for defining and using classes. In this article, we will cover the basics of classes in C#, including how to declare a class, create objects from a class, access properties and methods of an object, use constructors, and leverage inheritance and access modifiers. By the end of this article, you will have a comprehensive understanding of classes in C# and be able to leverage them to build powerful, object-oriented programs.

Classes are an essential part of object-oriented programming (OOP), which is a programming paradigm based on the concept of objects. In C#, classes are used to define objects and their properties, as well as the methods and events that operate on them.

A class is a blueprint or a template for creating objects of a specific type. It defines the properties and methods that are common to all objects of that type. For example, if you were creating a program that represented cars, you might define a class called "Car" that would include properties such as "Make", "Model", "Year", and "Color", as well as methods like "StartEngine", "Accelerate", and "Brake".

Declaring a Class in C#

To declare a class in C#, you use the "class" keyword, followed by the name of the class, and the class body enclosed in curly braces. Here is an example of a simple class in C#:

class Car
{
    public string Make;
    public string Model;
    public int Year;
    public string Color;
 
    public void StartEngine()
    {
        Console.WriteLine("Engine started");
    }
}

In this example, we have defined a class called "Car" that includes four properties and one method. The properties are all public, which means that they can be accessed from outside the class.

Creating Objects from a Class

To create an object from a class in C#, you use the "new" keyword, followed by the name of the class and any necessary arguments for the class constructor. Here is an example of how to create an object from the "Car" class:

Car myCar = new Car();

In this example, we have created an object called "myCar" from the "Car" class. This object has access to all the properties and methods defined in the "Car" class.

Accessing Properties and Methods of an Object

To access the properties and methods of an object in C#, you use the dot notation, where you specify the name of the object, followed by a dot, and then the name of the property or method. Here is an example of how to access the properties and methods of the "myCar" object:

myCar.Make = "Toyota";
myCar.Model = "Camry";
myCar.Year = 2021;
myCar.Color = "Red";
 
myCar.StartEngine();

In this example, we have set the properties of the "myCar" object to specific values, and then called the "StartEngine" method of the "myCar" object.

Constructors in C#

Constructors are special methods that are used to initialize objects when they are created. In C#, constructors have the same name as the class, and they do not have a return type. Here is an example of a constructor in the "Car" class:

class Car
{
    public string Make;
    public string Model;
    public int Year;
    public string Color;
 
    public Car(string make, string model, int year, string color)
    {
        Make = make;
        Model = model;
        Year = year;
        Color = color;
    }
 
    public void StartEngine()
    {
        Console.WriteLine("Engine started");
    }
}

In this example, we have added a constructor to the "Car" class that takes four arguments. When an object is created from the "Car" class, the constructor will be called automatically, and the values of the properties will be set to the values of the arguments.

Inheritance in C#

Inheritance allows you to create new classes based on existing classes, thereby reusing code and adding new functionality to the new class. In C#, you use the ":" operator to create an inheritance relationship between two classes. Here is an example of a class that inherits from the "Car" class:

class SportsCar : Car
{
    public bool Turbo;
 
    public void ActivateTurbo()
    {
        Console.WriteLine("Turbo activated");
    }
}

In this example, we have created a new class called "SportsCar" that inherits from the "Car" class. The "SportsCar" class includes a new property called "Turbo" and a new method called "ActivateTurbo". Because "SportsCar" inherits from "Car", it also has access to all the properties and methods defined in the "Car" class.

Access Modifiers in C#

Access modifiers in C# are used to control the visibility of properties, methods, and classes. There are four access modifiers in C#: public, private, protected, and internal.

Public members are accessible from anywhere, both inside and outside the class.

Private members are accessible only from within the class.

Protected members are accessible only from within the class and from derived classes.

Internal members are accessible only within the same assembly.

Here is an example of how to use access modifiers in C#:

class Car
{
    public string Make;
    private int mileage;
    protected int fuelLevel;
    internal string licensePlate;
 
    public void StartEngine()
    {
        Console.WriteLine("Engine started");
    }
}

In this example, we have used different access modifiers for each of the properties in the "Car" class. The "Make" property is public, the "mileage" property is private, the "fuelLevel" property is protected, and the "licensePlate" property is internal.

Conclusion

Classes are a fundamental concept in object-oriented programming, and C# provides a robust set of features for defining and using classes. In this article, we covered the basics of declaring a class, creating objects from a class, accessing properties and methods of an object, using constructors, inheritance, and access modifiers. By mastering these concepts, you will be able to create sophisticated programs that leverage the power of OOP.