C# Programming Exercises

C# Programming Exercises
Aktualisiert am 13.6.2025
Tobias Bueck

Tobias Bueck

Hello! I am Tobias Bueck, founder of Softoft. As a computer scientist, I share insights on my blog about software development, particularly the OTOBO ticket system, as well as automation tools like UIPath and Zapier.

Encapsulation of Properties and Constructors

Task 1

Encapsulate this class in a sensible way. If you don't know how to do this, learn more about Object-Oriented Programming in C#: Object-Oriented Programming in C#

class Mitarbeiter
{
    public string name;
    public string email;
    public double stundenlohn;
}

Implement a constructor, including appropriate plausibility checks, and create an object of this class. Verify that your checks work.

class Mitarbeiter { private readonly string _name; private string _email; private double _studenlohn;

public string Name { get { return _name; } }

public string Email { get { return _email; } set { if (String.IsNullOrWhiteSpace(value)) throw new Exception();

_email = value; } }

public double Studenlohn { get { return _studenlohn; } set { if (value ≤ 0.0) throw new Exception();

_studenlohn = value; } }

public Mitarbeiter(string name, string email, double stundenlohn) { if (String.IsNullOrWhiteSpace(name)) throw new Exception();

_name = name; Email = email; Studenlohn = stundenlohn; } }

Explanation

In C#, encapsulation is the ability to restrict access to certain properties and methods of a class. Encapsulation is used to control access to certain parts of a class and to ensure the integrity of the data.

There are two main forms of encapsulation in C#: public and private. Public properties and methods are accessible from outside the class and can be called by other classes and objects. Private properties and methods, on the other hand, are only accessible within the class and cannot be called from outside the class.

To encapsulate a property or method in C#, you must use the "private" or "public" keyword. Example:

In this example, the "Name" property is public and can be called by other classes and objects. The "Alter" property, on the other hand, is private and can only be called within the "Kunde" class.

It is important to note that encapsulation not only restricts access to properties and methods but also access to the data itself. This means that private properties and methods can only access the data within the class, while public properties and methods can access the data from outside the class.

Encapsulation is an important concept in object-oriented programming and helps to control access to certain parts of a class and ensure the integrity of the data. It allows developers to structure and organize the code of a class and ensure that it is used by other classes and objects only in the expected way.

Inheritance

Task

Create the appropriate classes:

A drink has a volume in ml (int) and a name (string). A hot drink is a drink and has a temperature (double). A cold drink is also a drink and contains a number of ice cubes (int). A lemonade is a cold drink and has a flavor (string). A fruit spritzer is also a cold drink and has a juice ratio and a sparkling water ratio (int). A coffee is a hot drink and has a variety.

Create the following objects in the process:

A cup of coffee of the variety "Robusta", is 97.5°C hot and has a volume of 120 ml.

A bottle of Fanta is a lemonade with orange flavor, 2 ice cubes, and a volume of 300 ml.

An apple juice spritzer is a fruit spritzer with a mix ratio of 80% sparkling water and 20% juice. It has a volume of 200 ml and 3 ice cubes.

Solution

Explanation

In C#, inheritance is the ability of a class to inherit from another class and take over its properties and methods. This allows developers to reuse existing code and transfer functionality from one class to another.

To inherit from a class in C#, you must use the ":" keyword, followed by the name of the class you want to inherit from. Example:

class Mitarbeiter {
  public string Name { get; set; }
  public int Alter { get; set; }
}

class Manager : Mitarbeiter {
  public string Abteilung { get; set; }
}

In this example, the "Manager" class inherits from the "Mitarbeiter" class and takes over its properties "Name" and "Alter". The "Manager" class also defines a new property called "Abteilung".

It is important to note that inheritance in C# also provides the ability to override or extend existing methods. This allows developers to customize the functionality of inheriting classes and adapt them to their specific requirements. To minimize dependencies between classes, you can also use Dependency Injection. Learn more about it here: Dependency Injection in C#

Inheritance is an important concept in object-oriented programming and allows developers to reuse code and transfer functionality from one class to another. It also helps to improve the structure and organization of code and enhances the maintainability and serviceability of applications.

To check if the code works, you can write unit tests. Learn more about it here: Unit Tests in C#