SlideShare a Scribd company logo
Lesson 5
Learn C#. Series of C# lessons
http://csharp.honcharuk.me/lesson-5
Agenda
• this
• Nested Types
• Structs
• Enums
• Guid
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);
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.
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
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
};
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());
Thank you!
Questions?

More Related Content

PDF
Feliz navidad hohoho
PDF
Ethical Issues in a Rural Context
PDF
Αναφορά Ν. Μηταράκη για τη μεγάλη καθυστέρηση στην απονομή συντάξεων
PPTX
Lesson 10
PPTX
Olympic athletes
PDF
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...
PDF
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...
Feliz navidad hohoho
Ethical Issues in a Rural Context
Αναφορά Ν. Μηταράκη για τη μεγάλη καθυστέρηση στην απονομή συντάξεων
Lesson 10
Olympic athletes
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...

Similar to Lesson5 (20)

PPTX
2. overview of c#
PPTX
gffhfghfgchfygnghS09-Design-Patterns.pptx
PPTX
Kotlin for all the Things
PDF
Testing the waters of iOS
PDF
Design patterns in the 21st Century
PPT
java tutorial 3
PPTX
Introduction to Kotlin for Android developers
PPTX
Oopsecondgrouppresentation 180726073512-converted (1)
PPTX
class as the basis.pptx
PPTX
Polymorphism in C++
PPSX
Java session4
PPTX
Kpi driven-java-development-fn conf
PPTX
Week9 Intro to classes and objects in Java
PDF
Design Patterns in the 21st Century - Samir Talwar
PDF
Effective Java and Kotlin
PDF
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
PDF
An Introduction to Property Based Testing
PDF
BangaloreJUG introduction to kotlin
PDF
Simpler Machine Learning with SKLL
PPTX
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
2. overview of c#
gffhfghfgchfygnghS09-Design-Patterns.pptx
Kotlin for all the Things
Testing the waters of iOS
Design patterns in the 21st Century
java tutorial 3
Introduction to Kotlin for Android developers
Oopsecondgrouppresentation 180726073512-converted (1)
class as the basis.pptx
Polymorphism in C++
Java session4
Kpi driven-java-development-fn conf
Week9 Intro to classes and objects in Java
Design Patterns in the 21st Century - Samir Talwar
Effective Java and Kotlin
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
An Introduction to Property Based Testing
BangaloreJUG introduction to kotlin
Simpler Machine Learning with SKLL
OOPJ_PPT1, Inheritance, Encapsulation Polymorphism Static Variables.pptx
Ad

More from Alex Honcharuk (7)

PPTX
Lesson11
PPTX
PPTX
PPTX
PPTX
Lesson 4
PPTX
PPTX
Ad

Recently uploaded (20)

PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
PPT on Performance Review to get promotions
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Artificial Intelligence
PPTX
Sustainable Sites - Green Building Construction
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPT
Mechanical Engineering MATERIALS Selection
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Current and future trends in Computer Vision.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Project quality management in manufacturing
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
introduction to datamining and warehousing
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT on Performance Review to get promotions
Embodied AI: Ushering in the Next Era of Intelligent Systems
Artificial Intelligence
Sustainable Sites - Green Building Construction
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Categorization of Factors Affecting Classification Algorithms Selection
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Mechanical Engineering MATERIALS Selection
Automation-in-Manufacturing-Chapter-Introduction.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Current and future trends in Computer Vision.pptx
573137875-Attendance-Management-System-original
Fundamentals of safety and accident prevention -final (1).pptx
III.4.1.2_The_Space_Environment.p pdffdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Project quality management in manufacturing
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
introduction to datamining and warehousing

Lesson5

  • 1. Lesson 5 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-5
  • 2. Agenda • this • Nested Types • Structs • Enums • Guid
  • 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}]"); } }
  • 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. 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());

Editor's Notes

  • #4: https://msdn.microsoft.com/en-us/library/dk1507sz.aspx
  • #5: https://msdn.microsoft.com/en-us/library/ms173120.aspx
  • #6: https://msdn.microsoft.com/uk-ua/library/aa288471(v=vs.71).aspx
  • #7: https://msdn.microsoft.com/en-us/library/sbbt4032.aspx
  • #8: https://msdn.microsoft.com/en-us/library/system.guid.newguid(v=vs.110).aspx