Tracks
/
C#
C#
/
Syllabus
/
Classes
Cl

Classes in C#

21 exercises

About Classes

The primary object-oriented construct in C# is the class, which is a combination of data (fields) and behavior (methods). The fields and methods of a class are known as its members.

Access to members can be restricted through access modifiers, the two most common ones being:

  • public: the member can be accessed by any code (no restrictions).
  • private: the member can only be accessed by code in the same class.

It is customary to specify an access modifier for all members. If no access modifier is specified, it will default to private.

The above-mentioned grouping of related data and behavior plus restricting access to members is known as encapsulation, which is one of the core object-oriented concepts.

The combination of field values of an object at any one time are known as the object's state.

You can think of a class as a template for creating instances of that class. To create an instance of a class (also known as an object), the new keyword is used:

class Car
{
}

// Create two car instances
var myCar = new Car();
var yourCar = new Car();

Fields have a type and can be defined anywhere in a class. Public fields are defined in PascalCase and private fields are defined in camelCase and prefixed with an underscore _:

class Car
{
    // Accessible by anyone
    public int Weight;

    // Only accessible by code in this class
    private string _color;
}

One can optionally assign an initial value to a field. If a field does not specify an initial value, it will be set to its type's default value. An instance's field values can be accessed and updated using dot-notation.

class Car
{
    // Will be set to specified value
    public int Weight = 2500;

    // Will be set to default value (0)
    public int Year;
}

var newCar = new Car();
newCar.Weight; // => 2500
newCar.Year;   // => 0

// Update value of the field
newCar.Year = 2018;

Private fields are usually updated as a side-effect of calling a method. Such methods usually don't return any value, in which case the return type should be void:

class CarImporter
{
    private int _carsImported;

    public void ImportCars(int numberOfCars)
    {
        // Update private field
        _carsImported = _carsImported + numberOfCars;
    }
}

Note that is not customary to use public fields in C# classes. Either private fields are used or other types of members that will be discussed in subsequent exercises.

Within a class, the this keyword will refer to the current class. This is especially useful if a parameter has the same name as a field:

class CarImporter
{
    private int carsImported;

    public void SetImportedCars(int carsImported)
    {
        // Update private field from public method
        this.carsImported = carsImported;
    }
}

The class and method structure provides a natural way to limit visibility of program elements such as fields and methods and avoid the well-known problem of global state. Public fields, methods and other members have to be qualified with the object name, object.field or object.method(), (class name in the case of statics) to be seen by code outside the class. Variables and parameters declared within a method can be seen only within that method.

One thing to be wary of with this approach is that if a field name and the name of a variable or parameter in a method are the same then the field name will be ignored by code within the method which may not be what you desire. In general, you should avoid this where possible although it is a common pattern in constructors. If it is appropriate for the field and variable name to be the same then qualify the field name with the this keyword within the method.

These access modifiers can be used to further limit visibility.

Edit via GitHub The link opens in a new window or tab

Learn Classes

Practicing is locked

Unlock 8 more exercises to practice Classes