3. this
• The this keyword refers to the current instance of the class
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;
}
}
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);
}
}
CoffeMachine cm = new CoffeMachine();
cm.MilkAvailable = true;
Coffe coffe = cm.GetCappuccino(false);
4. 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();
5. 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.
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}]");
}
}
7. Guid
• GUID - a globally unique identifier
Guid g;
// Create and display the value of two GUIDs.
g = Guid.NewGuid();
Console.WriteLine(g);
Console.WriteLine(Guid.NewGuid());