Let’s keep on learning more about C# data types.

Agenda

this

The this keyword refers to the current instance of the class. Calling this inside a class means that an object of its class being called. Let’s review a coffe machine example to understand it better:

Coffe class can create some coffe according to inputs from coffe machine. Please pay your attention how this being used.

class Coffe
{
    private CoffeMachine _coffeMachine;
    public Coffe(CoffeMachine coffeMachine)
    {
        _coffeMachine = coffeMachine;
    }

    public bool Shugar { get; set; }
    public bool Milk { get; set; }

    public Coffe Cappuccino(bool sugar)
    {
        if (_coffeMachine.MilkAvailable)
        {
            Shugar = sugar;

            Milk = true;

            return this;
        }

        return null;
    }
}

Coffe machine has its state (sugar and milk available) and supplies prepared coffe.

class CoffeMachine
{
    public bool ShugarAvailable { get; set; }
    public bool MilkAvailable { get; set; }

    public Coffe GetCappuccino(bool sugar)
    {
        Coffe coffe = new Coffe(this);

        return coffe.Cappuccino(ShugarAvailable);
    }
}

Coffe machine framework can be used like this:

CoffeMachine cm = new CoffeMachine();
cm.MilkAvailable = true;
Coffe coffe = cm.GetCappuccino(false);

Nested Types

Use a nested class when the class you are nesting is only useful to the enclosing class.

class Settings
{
    public class Screen
    {
        public int Brightness { get; set; }
        public string Wallpaper { get; set; }
    }

    public class Sound
    {
        public int Volume { get; set; }
        public bool Mute { get; set; }
    }

    public string UserName { get; set; }
    public Screen ScreenConfig { get; set; }
    public Sound SoundConfig { get; set; }
}
Settings settings = new Settings();
settings.ScreenConfig = new Settings.Screen();
settings.SoundConfig = new Settings.Sound();

Structs

Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types. Assignment to a variable of a struct type creates a copy of the value being assigned. A struct is not permitted to declare a parameterless instance constructor. A struct is not permitted to declare a destructor.

Structure example:

struct Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public void PrintCurrentPosition()
    {
        Console.WriteLine($"[{X}, {Y}]");
    }
}

Enums

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

Example:

enum Gender
{
    Male,
    Female,
    Undefined
}

enum Food
{
    Vegetarian,
    GlutenFree,
    NutFree,
    SoyFree,
    DairyFree,
    Organic,
    Kosher,
    FishFree
}

enum Alcohol
{
    None = 0,
    Light = 15,
    Medium = 25,
    Hot = 40
}

Actually enum is a representation of type int:

int value = (int)Food.NutFree; // returns 3

Class example with enums as a members:

class Passenger
{
    public Gender Gender { get; set; }
    public Food Food { get; set; }
    public Alcohol PrefferedAlcohol { get; set; }
}

And the usage of this calss. Please pay attention how object is initialized:

Passenger applicant = new Passenger
{
    Gender = Gender.Female,
    Food = Food.Kosher,
    PrefferedAlcohol = Alcohol.Hot
};

Guid

GUID - a globally unique identifier. Guid it’s a struct. Theoretically it’s impossible to generate two identical GUIDs in the world. Please refer the following example on how to generate a GUID value.

Guid g;
// Create and display the value of two GUIDs.
g = Guid.NewGuid();
Console.WriteLine(g);
Console.WriteLine(Guid.NewGuid());

References

Homework

  • Implement singleton design pattern class
  • Extend coffe machine example to support other coffe recipes

Presentation

Lesson5 from Alex Honcharuk