Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Lesson 10

135 Aufrufe

Veröffentlicht am

Learn C#

Veröffentlicht in: Ingenieurwesen
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Lesson 10

  1. 1. Lesson 10 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-10
  2. 2. Agenda • Exception handling • Operator overloading • Extension Methods
  3. 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. 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. 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. 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. 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(); } }
  8. 8. Thank you! Questions?

×