SlideShare a Scribd company logo
1 of 8
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

Similar to Lesson5

2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#Rohit Rao
 
Kotlin for all the Things
Kotlin for all the ThingsKotlin for all the Things
Kotlin for all the ThingsEamonn Boyle
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOSKremizas Kostas
 
Design patterns in the 21st Century
Design patterns in the 21st CenturyDesign patterns in the 21st Century
Design patterns in the 21st CenturySamir Talwar
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developersMohamed Wael
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Hassan Hashmi
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++Rabin BK
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Javakjkleindorfer
 
Design Patterns in the 21st Century - Samir Talwar
Design Patterns in the 21st Century - Samir TalwarDesign Patterns in the 21st Century - Samir Talwar
Design Patterns in the 21st Century - Samir TalwarJAXLondon2014
 
Effective Java and Kotlin
Effective Java and KotlinEffective Java and Kotlin
Effective Java and Kotlin경주 전
 
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica) Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica) Thinkful
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 
BangaloreJUG introduction to kotlin
BangaloreJUG   introduction to kotlinBangaloreJUG   introduction to kotlin
BangaloreJUG introduction to kotlinChandra Sekhar Nayak
 
Simpler Machine Learning with SKLL
Simpler Machine Learning with SKLLSimpler Machine Learning with SKLL
Simpler Machine Learning with SKLLDaniel Blanchard
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - InheritanceMichael Heron
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 

Similar to Lesson5 (20)

2. overview of c#
2. overview of c#2. overview of c#
2. overview of c#
 
Kotlin for all the Things
Kotlin for all the ThingsKotlin for all the Things
Kotlin for all the Things
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOS
 
Design patterns in the 21st Century
Design patterns in the 21st CenturyDesign patterns in the 21st Century
Design patterns in the 21st Century
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Introduction to Kotlin for Android developers
Introduction to Kotlin for Android developersIntroduction to Kotlin for Android developers
Introduction to Kotlin for Android developers
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Java session4
Java session4Java session4
Java session4
 
Kpi driven-java-development-fn conf
Kpi driven-java-development-fn confKpi driven-java-development-fn conf
Kpi driven-java-development-fn conf
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Design Patterns in the 21st Century - Samir Talwar
Design Patterns in the 21st Century - Samir TalwarDesign Patterns in the 21st Century - Samir Talwar
Design Patterns in the 21st Century - Samir Talwar
 
Effective Java and Kotlin
Effective Java and KotlinEffective Java and Kotlin
Effective Java and Kotlin
 
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica) Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
Build a Virtual Pet with JavaScript (May 2017, Santa Monica)
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based Testing
 
BangaloreJUG introduction to kotlin
BangaloreJUG   introduction to kotlinBangaloreJUG   introduction to kotlin
BangaloreJUG introduction to kotlin
 
Simpler Machine Learning with SKLL
Simpler Machine Learning with SKLLSimpler Machine Learning with SKLL
Simpler Machine Learning with SKLL
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 

More from Alex Honcharuk (7)

Lesson11
Lesson11Lesson11
Lesson11
 
Lesson9
Lesson9Lesson9
Lesson9
 
Lesson8
Lesson8Lesson8
Lesson8
 
Lesson6
Lesson6Lesson6
Lesson6
 
Lesson 4
Lesson 4Lesson 4
Lesson 4
 
Lesson2
Lesson2Lesson2
Lesson2
 
Lesson1
Lesson1Lesson1
Lesson1
 

Recently uploaded

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 

Recently uploaded (20)

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 

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

  1. https://msdn.microsoft.com/en-us/library/dk1507sz.aspx
  2. https://msdn.microsoft.com/en-us/library/ms173120.aspx
  3. https://msdn.microsoft.com/uk-ua/library/aa288471(v=vs.71).aspx
  4. https://msdn.microsoft.com/en-us/library/sbbt4032.aspx
  5. https://msdn.microsoft.com/en-us/library/system.guid.newguid(v=vs.110).aspx