Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lesson5

197 views

Published on

Learn C#

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Lesson5

  1. 1. Lesson 5 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-5
  2. 2. Agenda • this • Nested Types • Structs • Enums • Guid
  3. 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. 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. 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}]"); } }
  6. 6. Enums 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 } class Passenger { public Gender Gender { get; set; } public Food Food { get; set; } public Alcohol PrefferedAlcohol { get; set; } } Passenger applicant = new Passenger { Gender = Gender.Female, Food = Food.Kosher, PrefferedAlcohol = Alcohol.Hot };
  7. 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());
  8. 8. Thank you! Questions?

×