SlideShare a Scribd company logo
1 of 8
Lesson 10
Learn C#. Series of C# lessons
http://csharp.honcharuk.me/lesson-10
Agenda
• Exception handling
• Operator overloading
• Extension Methods
try-catch
try
{
//ToDo: your code goes here
}
catch
{
//ToDo: if something goes wrong,
//ToDo: this block of code will be executed
}
while (true)
{
Console.WriteLine("Calculation started.");
try
{
Console.Write("Enter a: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter b: ");
int b = int.Parse(Console.ReadLine());
Console.WriteLine($"{a}/{b} = {a/b}");
}
catch
{
Console.WriteLine("Operation failed.");
}
Console.WriteLine("Would you like continue calculations? y/n");
if (Console.ReadLine().ToLower() != "y")
{
break;
}
}
Console.WriteLine("Good bye!");
try-catch-finally
while (true)
{
Console.WriteLine("Calculation started.");
try
{
Console.Write("Enter a: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter b: ");
int b = int.Parse(Console.ReadLine());
Console.WriteLine($"{a}/{b} = {a/b}");
}
catch (Exception e)
{
Console.WriteLine("Operation failed. Message: {0}", e);
}
finally
{
Console.WriteLine("Current calculation completed.");
}
Console.WriteLine("Would you like continue calculations? y/n");
if (Console.ReadLine().ToLower() != "y")
{
break;
}
}
Your own exception type and throw keyword
class AttendanceException : Exception
{
public AttendanceException()
{
}
public AttendanceException(string message) : base(message)
{
}
public AttendanceException(string message, Exception
innerException) : base(message, innerException)
{
}
protected AttendanceException(SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
int pupilsNumber = 0;
try
{
pupilsNumber = int.Parse(Console.ReadLine());
if (pupilsNumber == 0)
{
throw new AttendanceException("Everybody absent");
}
}
catch (AttendanceException)
{
Console.WriteLine("Lesson is ignored :(");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine($"{pupilsNumber} pupils here");
Operator overloading
class Material
{
public int Weight { get; set; }
public string Name { get; set; }
public static Material operator + (Material m1,
Material m2)
{
return new Material
{
Name = m1.Name+" and "+ m2.Name,
Weight = m1.Weight + m2.Weight
};
}
}
Material water = new Material
{
Name = "Water",
Weight = 120
};
Material salt = new Material
{
Name = "Salt",
Weight = 15
};
Material solterWater = water + salt;
Console.WriteLine($"{solterWater.Name}, {solterWater.Weight}");
Extension Methods
public static class IntExtensions
{
public static int Increment(this int n, int amount)
{
return n + amount;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(10.Increment(3));
Console.ReadLine();
}
}
Thank you!
Questions?

More Related Content

Viewers also liked

Olympic athletes
Olympic athletesOlympic athletes
Olympic athletesMar Jurado
 
Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...
Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...
Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...Notis Mitarachi
 
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...Notis Mitarachi
 
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...Notis Mitarachi
 
SaaS partnerships, business application marketplaces and ecosystem growth str...
SaaS partnerships, business application marketplaces and ecosystem growth str...SaaS partnerships, business application marketplaces and ecosystem growth str...
SaaS partnerships, business application marketplaces and ecosystem growth str...Judy Loehr
 
Moodle LMS Overview
Moodle LMS OverviewMoodle LMS Overview
Moodle LMS OverviewSteve Rayson
 

Viewers also liked (9)

Menú
MenúMenú
Menú
 
Olympic athletes
Olympic athletesOlympic athletes
Olympic athletes
 
Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...
Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...
Απάντηση Ν. Τόσκα σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έναρξ...
 
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...
Απάντηση Ε. Κουντουρά σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης έ...
 
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...
Απάντηση Π. Κουρουμπλή σε αναφορά Ν. Μηταράκη σχετικά με την ανάγκη έγκαιρης ...
 
SaaS partnerships, business application marketplaces and ecosystem growth str...
SaaS partnerships, business application marketplaces and ecosystem growth str...SaaS partnerships, business application marketplaces and ecosystem growth str...
SaaS partnerships, business application marketplaces and ecosystem growth str...
 
Linear svm
Linear svmLinear svm
Linear svm
 
Moodle LMS Overview
Moodle LMS OverviewMoodle LMS Overview
Moodle LMS Overview
 
Kayan 2015
Kayan  2015Kayan  2015
Kayan 2015
 

Similar to Lesson 10

Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptxTamas Rev
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chhom Karath
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime
 
Concurrency in go
Concurrency in goConcurrency in go
Concurrency in goborderj
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitAndrey Karpov
 
What static analyzers can do that programmers and testers cannot
What static analyzers can do that programmers and testers cannotWhat static analyzers can do that programmers and testers cannot
What static analyzers can do that programmers and testers cannotAndrey Karpov
 
Java best practices
Java best practicesJava best practices
Java best practicesRay Toal
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDBLee Theobald
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test communityKerry Buckley
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyGrejoJoby1
 
Html Server Input Checkbox Control CS
Html Server Input Checkbox Control CSHtml Server Input Checkbox Control CS
Html Server Input Checkbox Control CSsunmitraeducation
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheeltcurdt
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersRick Beerendonk
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...PVS-Studio
 

Similar to Lesson 10 (20)

Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Loops
LoopsLoops
Loops
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
C # (2)
C # (2)C # (2)
C # (2)
 
Chapter i c#(console application and programming)
Chapter i c#(console application and programming)Chapter i c#(console application and programming)
Chapter i c#(console application and programming)
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
 
Concurrency in go
Concurrency in goConcurrency in go
Concurrency in go
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGit
 
What static analyzers can do that programmers and testers cannot
What static analyzers can do that programmers and testers cannotWhat static analyzers can do that programmers and testers cannot
What static analyzers can do that programmers and testers cannot
 
Java best practices
Java best practicesJava best practices
Java best practices
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDB
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test community
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Html Server Input Checkbox Control CS
Html Server Input Checkbox Control CSHtml Server Input Checkbox Control CS
Html Server Input Checkbox Control CS
 
Apache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheelApache Commons - Don\'t re-invent the wheel
Apache Commons - Don\'t re-invent the wheel
 
C#6 - The New Stuff
C#6 - The New StuffC#6 - The New Stuff
C#6 - The New Stuff
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
JavaScript 2016 for C# Developers
JavaScript 2016 for C# DevelopersJavaScript 2016 for C# Developers
JavaScript 2016 for C# Developers
 
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
Comparing the general static analysis in Visual Studio 2010 and PVS-Studio by...
 

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

Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate productionChinnuNinan
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectErbil Polytechnic University
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Romil Mishra
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfDrew Moseley
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfisabel213075
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Cooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptxCooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptxmamansuratman0253
 

Recently uploaded (20)

Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate production
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdf
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Cooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptxCooling Tower SERD pH drop issue (11 April 2024) .pptx
Cooling Tower SERD pH drop issue (11 April 2024) .pptx
 

Lesson 10

  • 1. Lesson 10 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-10
  • 2. Agenda • Exception handling • Operator overloading • Extension Methods
  • 3. try-catch try { //ToDo: your code goes here } catch { //ToDo: if something goes wrong, //ToDo: this block of code will be executed } while (true) { Console.WriteLine("Calculation started."); try { Console.Write("Enter a: "); int a = int.Parse(Console.ReadLine()); Console.Write("Enter b: "); int b = int.Parse(Console.ReadLine()); Console.WriteLine($"{a}/{b} = {a/b}"); } catch { Console.WriteLine("Operation failed."); } Console.WriteLine("Would you like continue calculations? y/n"); if (Console.ReadLine().ToLower() != "y") { break; } } Console.WriteLine("Good bye!");
  • 4. try-catch-finally while (true) { Console.WriteLine("Calculation started."); try { Console.Write("Enter a: "); int a = int.Parse(Console.ReadLine()); Console.Write("Enter b: "); int b = int.Parse(Console.ReadLine()); Console.WriteLine($"{a}/{b} = {a/b}"); } catch (Exception e) { Console.WriteLine("Operation failed. Message: {0}", e); } finally { Console.WriteLine("Current calculation completed."); } Console.WriteLine("Would you like continue calculations? y/n"); if (Console.ReadLine().ToLower() != "y") { break; } }
  • 5. Your own exception type and throw keyword class AttendanceException : Exception { public AttendanceException() { } public AttendanceException(string message) : base(message) { } public AttendanceException(string message, Exception innerException) : base(message, innerException) { } protected AttendanceException(SerializationInfo info, StreamingContext context) : base(info, context) { } } int pupilsNumber = 0; try { pupilsNumber = int.Parse(Console.ReadLine()); if (pupilsNumber == 0) { throw new AttendanceException("Everybody absent"); } } catch (AttendanceException) { Console.WriteLine("Lesson is ignored :("); } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine($"{pupilsNumber} pupils here");
  • 6. Operator overloading class Material { public int Weight { get; set; } public string Name { get; set; } public static Material operator + (Material m1, Material m2) { return new Material { Name = m1.Name+" and "+ m2.Name, Weight = m1.Weight + m2.Weight }; } } Material water = new Material { Name = "Water", Weight = 120 }; Material salt = new Material { Name = "Salt", Weight = 15 }; Material solterWater = water + salt; Console.WriteLine($"{solterWater.Name}, {solterWater.Weight}");
  • 7. Extension Methods public static class IntExtensions { public static int Increment(this int n, int amount) { return n + amount; } } class Program { static void Main(string[] args) { Console.WriteLine(10.Increment(3)); Console.ReadLine(); } }