SlideShare une entreprise Scribd logo
1  sur  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?

Contenu connexe

En vedette

En vedette (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
 

Similaire à Lesson 10

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_csharp4
Abed 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
 
Introduction To MongoDB
Introduction To MongoDBIntroduction To MongoDB
Introduction To MongoDB
Lee Theobald
 
Tdd for BT E2E test community
Tdd for BT E2E test communityTdd for BT E2E test community
Tdd for BT E2E test community
Kerry Buckley
 

Similaire à 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...
 

Plus de 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
 

Dernier

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Dernier (20)

Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 

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(); } }