Your SlideShare is downloading. ×
0
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
C sharp part2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

C sharp part2

940

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
940
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
64
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Introduction to Programming in .Net Environment Masoud Milani
  • 2. Programming in C# Part 2
  • 3. Overview <ul><li>Classes </li></ul><ul><li>Inheritance </li></ul><ul><li>Abstract classes </li></ul><ul><li>Sealed classes </li></ul><ul><li>Files </li></ul><ul><li>Delegates </li></ul><ul><li>Interfaces </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 4. Classes <ul><li>Members </li></ul><ul><ul><li>Constants </li></ul></ul><ul><ul><li>Fields </li></ul></ul><ul><ul><li>Methods </li></ul></ul><ul><ul><ul><li>parameters </li></ul></ul></ul><ul><ul><li>Properties </li></ul></ul><ul><ul><li>Indexer </li></ul></ul><ul><ul><li>Operators </li></ul></ul><ul><ul><li>Constructors </li></ul></ul><ul><ul><li>Destructors </li></ul></ul><ul><li>Overloading </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 5. Parameters Passing <ul><li>Pass by value </li></ul><ul><ul><li>Value types are passed by value by default </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>int Smallest(int a, int b) { if (a &gt; b) return b; else return a; } <ul><li>Change in a value parameter is not reflected in the caller </li></ul>
  • 6. Parameters Passing <ul><li>Pass by reference </li></ul><ul><ul><li>out (output) </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>void Smaller(int a, int b, out int result) { result=a&gt;b?b: a; } <ul><li>Change in a out parameter is reflected in the caller </li></ul><ul><li>The function can not use the value of an out parameter before an assignment is made to it. </li></ul>int a=20; int b=30; Smaller(a, b, out sm); sm=20
  • 7. Parameters Passing <ul><li>Pass by reference </li></ul><ul><ul><li>ref (input and output) </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>void Swap(ref int a, ref int b) { int t=a; a=b; b=t; } <ul><li>Change in a ref parameter is reflected in the caller </li></ul><ul><li>The function can use the value of a reference parameter before an assignment is made to it. </li></ul>int a=20; int b=30; Swap(ref a, ref b); a=30 b=20
  • 8. Parameters Passing <ul><li>Reference types are passed by reference by default </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class ParamClass { public int myValue; } ParamClass p=new ParamClass(); p.myValue=9; Console.WriteLine(&amp;quot;Before Increment, p.myValue={0}&amp;quot;, p.myValue); Increment(p); Console.WriteLine(&amp;quot;After Increment, p.myValue={0}&amp;quot;, p.myValue); static void Increment(ParamClass q) { q.myValue++; } Before Increment, p.myValue=9 After Increment, p.myValue=10
  • 9. Variable Number of Parameters 1- <ul><ul><li>©Masoud Milani </li></ul></ul>int a=Smallest(5, 4, -3, 45, 2); int b=Smallest(1, 2, 3); int Smallest(params int[] a) { int smallest=int.MaxValue; foreach (int x in a) if (x &lt; smallest) smallest=x; return smallest; }
  • 10. Method Overloading <ul><li>More than one method with the same name can be defined for the same class </li></ul><ul><ul><li>methods must have different signatures </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 11. Operator Overloading <ul><li>More than one operator with the same name can be defined for the same class </li></ul><ul><ul><li>Overloaded operators must be </li></ul></ul><ul><ul><ul><li>public </li></ul></ul></ul><ul><ul><ul><li>Static </li></ul></ul></ul><ul><ul><li>Precedence and associativity of the operators can not be changed. </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 12. Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { public MyType(int v) { myValue=v; } public int myValue; public static MyType operator + (MyType x, MyType y) { return new MyType(x.myValue + y.myValue); } … } MyType p(5); MyType q(6); MyType s=p+q; s += p;
  • 13. Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { … public static MyType operator + (MyType x, int y) { return new MyType(x.myValue + y); } … } MyType p(5); MyType s=p+7; s += 7
  • 14. Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { … public static MyType operator ++ (MyType x) { x.myValue += 1; return new MyType(x.myValue + 1); } … } MyType p(5); MyType s=p++;
  • 15. Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { … public static bool operator true(MyType x) { return x.myValue !=0; } public static bool operator false (MyType x) { return x.myValue == 0; } … } MyType z(5); if (z) Console.WriteLine(&amp;quot;true&amp;quot;); else Console.WriteLine(&amp;quot;false&amp;quot;);
  • 16. Indexers <ul><li>Indexers are properties that allow objects to be accessed like arrays </li></ul><ul><ul><li>The accessor functions for indexers take additional parameter(s) for index </li></ul></ul><ul><ul><li>Indexers can not be declared static </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>Exams ex=new Exams(); ex [1] =100; Indexer
  • 17. Indexer 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Exams { public int this[int examNo] { get { … } set { … } } Exams ex=new Exams(); ex[1]=100; ex[2]=50; int average=ex[1] + ex[2];
  • 18. Example <ul><li>using System; /// &lt;summary&gt; ///     A simple indexer example. /// &lt;/summary&gt; class IntIndexer {     private string[] myData;     public IntIndexer(int size)     {         myData = new string[size];         for (int i=0; i &lt; size; i++)         {             myData[i] = &amp;quot;empty&amp;quot;;         }     }     </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 19. Example <ul><li>public string this[int pos]     {         get        {             return myData[pos];         }         set        {             myData[pos] = value;         }     }     </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>Should use this keyword
  • 20. Example <ul><li>static void Main(string[] args)     {         int size = 10;         IntIndexer myInd = new IntIndexer(size);         myInd[9] = &amp;quot;Some Value&amp;quot;;         myInd[3] = &amp;quot;Another Value&amp;quot;;         myInd[5] = &amp;quot;Any Value&amp;quot;;         Console.WriteLine(&amp;quot;nIndexer Outputn&amp;quot;);         for (int i=0; i &lt; size; i++)         {             Console.WriteLine(&amp;quot;myInd[{0}]: {1}&amp;quot;, i, myInd[i]);         }     } } </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 21. Output <ul><li>Indexer Output </li></ul><ul><li>myInd[0]: empty </li></ul><ul><li>myInd[1]: empty </li></ul><ul><li>myInd[2]: empty </li></ul><ul><li>myInd[3]: Another Value </li></ul><ul><li>myInd[4]: empty </li></ul><ul><li>myInd[5]: Any Value </li></ul><ul><li>myInd[6]: empty </li></ul><ul><li>myInd[7]: empty </li></ul><ul><li>myInd[8]: empty </li></ul><ul><li>myInd[9]: Some Value </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 22. Exercise <ul><li>Write a class Exams that have </li></ul><ul><ul><li>3 private integer fields exam1, exam2 and exam3 </li></ul></ul><ul><ul><li>An indexer that sets or gets the value of each exam </li></ul></ul><ul><ul><li>A public property Average that returns the average of the three exams </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 23. Exams class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Exams { public Exams(int ex1, int ex2, int ex3) { … } private int exam1; private int exam2; private int exam3; public double Average { … } }
  • 24. Inheritance <ul><li>Implements isA relation </li></ul><ul><ul><li>Student isA Person </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Person { public Person(string fn, string ln) { firstName=fn; lastName=ln; } private string firstName; private string lastName; } public class Student : Person { public Student(string fn, string ln, string mj): base(fn, ln) { major=mj; } private string major; }
  • 25. Inheritance <ul><li>A subclass can hide the inherited members of its superclass using the keyword new </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Person { … public void Show() { Console.Write(&amp;quot;Name: {0}, {1}&amp;quot;, lastName, firstName)} } <ul><li>public class Student : Person </li></ul><ul><li>{ </li></ul><ul><li>… </li></ul><ul><ul><li>public new void Show() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>base.Show(); </li></ul></ul><ul><ul><li>Console.Write(&amp;quot;major: {0}&amp;quot;, major); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  • 26. Inheritance 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Person p = new Person(&amp;quot;P1-F&amp;quot;, &amp;quot;P1-L&amp;quot;); p.Show(); Console.WriteLine(); Student s=new Student(&amp;quot;S1-F&amp;quot;, &amp;quot;S1-L&amp;quot;, &amp;quot;CS&amp;quot;); s.Show(); Console.WriteLine(); p=s; p.Show(); Name: P1-L, P1-F Name: S1-L, S1-F Name: S1-L, S1-F Major: CS
  • 27. Virtual Function <ul><li>A class can allow its subclasses to override its member functions by declaring them virtual </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Person { … public virtual void Show() { Console.Write(&amp;quot;Name: {0}, {1}&amp;quot;, lastName, firstName)} } <ul><li>public class Student : Inheritance.Person </li></ul><ul><li>{ </li></ul><ul><li>… </li></ul><ul><ul><li>public override void Show() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>base.Show(); </li></ul></ul><ul><ul><li>Console.Write(&amp;quot;major: {0}&amp;quot;, major); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>using namespace the override keyword is also required.
  • 28. Virtual Function 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Person p = new Person(&amp;quot;P1-F&amp;quot;, &amp;quot;P1-L&amp;quot;); p.Show(); Console.WriteLine(); Student s=new Student(&amp;quot;S1-F&amp;quot;, &amp;quot;S1-L&amp;quot;, &amp;quot;CS&amp;quot;); s.Show(); Console.WriteLine(); p=s; p.Show(); Name: S1-L, S1-F Major: CS Name: S1-L, S1-F Major: CS Name: P1-L, P1-F
  • 29. Sealed Modifier <ul><li>A sealed class is a class that can not be the base of any other class </li></ul><ul><ul><li>used to prevent inheritance from the class to facilitate future class modification </li></ul></ul><ul><li>A sealed method overrides an inherited virtual method with the same signature </li></ul><ul><ul><li>Used to prevent further overriding of the method </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 30. Example <ul><li>using System; </li></ul><ul><li>sealed class MyClass </li></ul><ul><li>{ </li></ul><ul><li>public int x; </li></ul><ul><li>public int y; </li></ul><ul><li>} </li></ul><ul><li>class MainClass </li></ul><ul><li>{ </li></ul><ul><li>public static void Main() </li></ul><ul><li>{ </li></ul><ul><li>MyClass mC = new MyClass(); </li></ul><ul><li>mC.x = 110; </li></ul><ul><li>mC.y = 150; </li></ul><ul><li>Console.WriteLine(&amp;quot;x = {0}, y = {1}&amp;quot;, mC.x, mC.y); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 31. Example <ul><li>class MyDerivedC: MyClass { } </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>Complation error!
  • 32. Abstract Classes <ul><li>An abstract class is a class that can not be instantiated </li></ul><ul><ul><li>Abstract classes are used for reuse and structural organization of the program </li></ul></ul><ul><ul><li>Abstract classes can have abstract or specified methods </li></ul></ul><ul><ul><ul><li>Non-abstract subclasses must override the abstract methods that they inherit </li></ul></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 33. Abstract Classes <ul><li>Software requirement is given: </li></ul><ul><ul><li>There is no object that is only a person. Each person is either a student or a graduate student </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 34. Abstract Classes 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public abstract class Person { public Person(string fn, string ln) { firstName=fn; lastName=ln; } private string firstName; private string lastName; public virtual void Show() { Console.Write(&amp;quot;Name: {0}, {1}&amp;quot;, lastName, firstName); } } Error: Abstract classes can not be instantiated Person p(“FN-1”, “LN-1”);
  • 35. Exercise 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Courses exam1 exam2 exam3 Average takes Class Grad Student GradStudent( … ) bool pass // &gt;80 void override Show() class Student string major double Average Student( … ) bool Pass // &gt;60 void override Show() Abstract class Person string fName string lName Person(string fn, string ln) void virtual Show() isA isA
  • 36. Exercise <ul><li>Ask the number of students </li></ul><ul><li>For each student ask </li></ul><ul><ul><li>First Name </li></ul></ul><ul><ul><li>Last Name </li></ul></ul><ul><ul><li>Major </li></ul></ul><ul><ul><li>Scores for each of three exams </li></ul></ul><ul><ul><li>Whether or not this is a graduate student </li></ul></ul><ul><ul><li>Create an appropriate student object and store it in an array </li></ul></ul><ul><li>Call Show member of each array entry </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 37. Person class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public abstract class Person { public Person(string fn, string ln) { … } private string firstName; private string lastName; public virtual void Show() { … } }
  • 38. Student class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Student : Inheritance.Person { public Student(string fn, string ln, string mj, int ex1, int ex2, int ex3):base(fn, ln) { … } private string major; public override void Show() { … } public virtual bool Pass { get { … } } protected Exams scores; public double Average { get { … } } }
  • 39. GradStudent class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class GradStudent : Inheritance.Student { public GradStudent(string fn, string ln, string mj, int ex1, int ex2, int ex3): base(fn, ln, mj, ex1, ex2, ex3) { … } public override bool Pass { get { … } } }
  • 40. Files <ul><li>A file is a sequential sequence of bytes </li></ul><ul><li>Applicable classes </li></ul><ul><ul><li>Text Input </li></ul></ul><ul><ul><ul><li>StreamReader </li></ul></ul></ul><ul><ul><li>Text Output </li></ul></ul><ul><ul><ul><li>StreamWriter </li></ul></ul></ul><ul><ul><li>Input/Output </li></ul></ul><ul><ul><ul><li>FileStream </li></ul></ul></ul><ul><li>NameSpace </li></ul><ul><ul><li>System.IO </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 41. StreamWriter 1- <ul><ul><li>©Masoud Milani </li></ul></ul>void WriteText() { int[] data=new int[5]{1,2,3,4,5}; StreamWriter outFile=new StreamWriter(&amp;quot;myOutFile.txt&amp;quot;); for(int i=0; i&lt;data.Length; i++) outFile.WriteLine(&amp;quot;data[{0}]={1}&amp;quot;, i, data[i]); outFile.Close(); } <ul><li>myOutFile: </li></ul><ul><ul><li>data[0]=1 </li></ul></ul><ul><ul><li>data[1]=2 </li></ul></ul><ul><ul><li>data[2]=3 </li></ul></ul><ul><ul><li>data[3]=4 </li></ul></ul><ul><ul><li>data[4]=5 </li></ul></ul>
  • 42. StreamReader <ul><li>Similar to StreamWriter </li></ul><ul><ul><li>Check for end of stream using the method Peek() </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>StreamReader inpTextFile=new StreamReader(&amp;quot;myOutFile.txt&amp;quot;); while(inpTextFile.Peek() &gt;=0) { string nextLine=inpTextFile.ReadLine(); Console.WriteLine(int.Parse(nextLine.Substring(nextLine.IndexOf(&amp;quot;=&amp;quot;)+1))); } inpTextFile.Close();
  • 43. Exercise <ul><li>Modify the previous program to save and retrieve information </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 44. FileStream <ul><li>To open a FileStream, we need to specify </li></ul><ul><ul><li>Physical path </li></ul></ul><ul><ul><li>File Mode </li></ul></ul><ul><ul><li>File Access </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 45. FileMode Enumeration <ul><li>Append </li></ul><ul><ul><li>Opens the file if it exists and seeks to the end of the file, or creates a new file </li></ul></ul><ul><li>Create </li></ul><ul><ul><li>A new file should be created. If the file already exists, it will be overwritten. </li></ul></ul><ul><li>Open </li></ul><ul><ul><li>An existing file should be opened </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 46. FileAccess Enumeration <ul><li>Read </li></ul><ul><ul><li>Read access </li></ul></ul><ul><li>ReadWrite </li></ul><ul><ul><li>Read and write access </li></ul></ul><ul><li>Write </li></ul><ul><ul><li>Write access </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 47. Formatter <ul><li>Direct reading to and Writing from FileStreams is very difficult </li></ul><ul><ul><li>Must read and write byte by byte </li></ul></ul><ul><li>Use a formatter to format objects that are to be written to the FileStream </li></ul><ul><ul><li>BinaryFormatter </li></ul></ul><ul><ul><ul><li>Formats objects in binary </li></ul></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 48. Formatter <ul><li>Methods </li></ul><ul><ul><li>Writing </li></ul></ul><ul><ul><ul><li>Serialize </li></ul></ul></ul><ul><ul><li>Reading </li></ul></ul><ul><ul><ul><li>Deserialize </li></ul></ul></ul><ul><ul><li>Exceptions: </li></ul></ul><ul><ul><ul><li>SerializationException </li></ul></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 49. Serialization 1- <ul><ul><li>©Masoud Milani </li></ul></ul>string myString=&amp;quot;string1&amp;quot;; int myInt=16; int[] myData=new int[5]{1,2,3,4,5}; FileStream f=new FileStream(&amp;quot;bFile&amp;quot;, FileMode.Create, FileAccess.Write); BinaryFormatter formatter=new BinaryFormatter(); formatter.Serialize(f, myString); formatter.Serialize(f, myInt); formatter.Serialize(f, myData); f.Close();
  • 50. <ul><ul><li>[Serializable()] </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 51. Deserialization 1- <ul><ul><li>©Masoud Milani </li></ul></ul>string myString=&amp;quot;string1&amp;quot;; int myInt=16; int[] myData=new int[5]{1,2,3,4,5}; f=new FileStream(&amp;quot;bFile&amp;quot;, FileMode.Open, FileAccess.Read); myString=(string)formatter.Deserialize(f); myInt=(int)formatter.Deserialize(f); newdata=(int[])formatter.Deserialize(f); Console.WriteLine(&amp;quot;myString={0}&amp;quot;, myString); Console.WriteLine(&amp;quot;myInt={0}&amp;quot;, myInt);
  • 52. Exercise <ul><li>Modify the previous program to save and retrieve information using a Binary Formatter </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 53. Exception Handling <ul><li>Exceptions are raised when a program encounters an illegal computation </li></ul><ul><ul><li>Division by zero </li></ul></ul><ul><ul><li>Array index out of range </li></ul></ul><ul><ul><li>Read beyond end of file </li></ul></ul><ul><ul><li>… . </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 54. Exception Handling <ul><li>A method that is unable to perform a computation throws an exception object </li></ul><ul><li>Exceptions can be caught by appropriate routines called exception handlers </li></ul><ul><li>Exception handlers receive the exception object containing information regarding the error </li></ul><ul><li>Exception objects are instances of classes that are derived from Exception class </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 55. Exception Handling <ul><li>try statement allows for catching exceptions in a block of code </li></ul><ul><li>try statement has one or more catch clauses to catch exceptions that are raised in the protected block </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>int[] a= new int[20]; try { Console.WriteLine(a[20]); } catch (Exception e) { Console.WriteLine(e.Message); } Index was outside the bounds of the array
  • 56. Exception Handling <ul><li>A try block can have multiple catch clauses </li></ul><ul><ul><li>The catch clauses are searched to find the first clause with a parameter that matches the thrown exception </li></ul></ul><ul><li>The program continues execution with the first statement that follows the try-catch-finally construct </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 57. Exception Handling <ul><li>try statement has an optional finally clause that executes </li></ul><ul><ul><li>When an exception is thrown and the corresponding catch clause is executed </li></ul></ul><ul><ul><li>When the protected block executes without raising throwing exception </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 58. Exception Handling 1- <ul><ul><li>©Masoud Milani </li></ul></ul>int[] a= new int[20]; try { Console.WriteLine(a[2]); } catch (Exception e) { Console.WriteLine(e.Message); } finally { Console.WriteLine(“Finally we are done!&amp;quot;); } Finally we are done!
  • 59. Exception Handling <ul><li>Exception classes </li></ul><ul><ul><li>IndexOutOfRangeException Class  </li></ul></ul><ul><ul><ul><li>when an attempt is made to access an element of an array with an index that is outside the bounds of the array. This class cannot be inherited </li></ul></ul></ul><ul><ul><li>DivideByZeroException Class </li></ul></ul><ul><ul><ul><li>The exception that is thrown when there is an attempt to divide an integral or decimal value by zero. </li></ul></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 60. Exception Handling <ul><li>Exception classes </li></ul><ul><ul><li>InvalidCastException Class </li></ul></ul><ul><ul><ul><li>The exception that is thrown for invalid casting or explicit conversion </li></ul></ul></ul><ul><ul><li>EndOfStreamException Class </li></ul></ul><ul><ul><ul><li>The exception that is thrown when reading is attempted past the end of a stream </li></ul></ul></ul><ul><li>For a list of Exception classes search for “SystemException Hierarchy” in MSDN.Net </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 61. Programmer Defined Exceptions <ul><li>Programmer defined exception classes must inherit from </li></ul><ul><ul><li>ApplicationException class </li></ul></ul><ul><li>Provide at least one constructor to set the message property </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyException : System.ApplicationException { public MyException(string message):base(message) { } }
  • 62. Programmer Defined Exceptions 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public static int g(int a, int b) { // adds positive numbers if (a &lt;0) { MyException e=new MyException(&amp;quot;1st parameter is negative&amp;quot;); throw(e); } if (b &lt;0) { MyException e=new MyException(&amp;quot;2nd dparameter is negative&amp;quot;); throw(e); } return a+b; } try { Console.WriteLine(&amp;quot;g(4, -5)={0}&amp;quot;, g(4, -5)); } catch (Exception e) Console.WriteLine(e.Message); }
  • 63. Delegates <ul><li>A delegate is a class that has only one or more methods </li></ul><ul><ul><li>Allows methods to be treated like objects </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>delegate int D1(int i, int j);
  • 64. Delegates 1- <ul><ul><li>©Masoud Milani </li></ul></ul>delegate int D1(int i, int j); public static int add(int a, int b) { Console.WriteLine(a+b); return a+b; } public static int mult(int a, int b) { Console.WriteLine(a*b); return a*b } static void Main(string[] args) { D1 d1=new D1(add); D1 d2=new D1(mult); d1=d1 + d2; d1(5, 5); } 10 25
  • 65. Delegates 1- <ul><ul><li>©Masoud Milani </li></ul></ul>delegate int D1(int i, int j); public static int add(int a, int b) { Console.WriteLine(a+b); return a+b; } public static int mult(int a, int b) { Console.WriteLine(a*b); return a*b } static void Main(string[] args) { D1 d1=new D1(add); D1 d2=new D1(mult); d1=d1 + d2; f(d1); } 10 25 public static void f(D1 d) { d(5,5); }
  • 66. Exercise <ul><li>Write a sort method that accepts an array of integer and a boolean delegate, compare, and sorts the array according to the delegate </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>public static void sort(int[] data, CompareDelegate Compare) { for (int i=0; i&lt;data.Length; ++i) for(int j=i+1; j&lt;data.Length; j++) if (Compare(data[i],data[j])) { int t=data[i]; data[i]=data[j]; data[j]=t; } }
  • 67. Interfaces <ul><li>An interface specifies the members that must be provided by classes that implement them </li></ul><ul><li>An Interface can define methods, properties, events, and indexers </li></ul><ul><li>The interface itself does not provide implementations for the members that it defines </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  • 68. Interfaces <ul><li>To add an interface, add a class and change it in the editor </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>interface IPublication { string Title { get; set; } string Publisher { get; set; } void Display(); }
  • 69. Interfaces 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Book: IPublication { public Book(string title, string author, string publisher) { Title=title; Author=author; Publisher=publisher; } private string author; private string title; private string publisher; public string Author { … } public string Title { … } public string Publisher { … } public void Display() { … } }
  • 70. Interfaces 1- <ul><ul><li>©Masoud Milani </li></ul></ul>static public void Display(IPublication[] p) { for (int i=0; i&lt;p.Length; ++i) p[i].Display(); } static void Main(string[] args) { IPublication[] publications= new IPublication[2]; publications[0] = new Book (&amp;quot;t0&amp;quot;, &amp;quot;a0&amp;quot;, &amp;quot;p0&amp;quot;); publications[1] = new Magazine (&amp;quot;t1&amp;quot;, &amp;quot;a1&amp;quot;); Display(publications); } public class Magazine : Interfaces.IPublication { … }
  • 71. Interfaces <ul><li>An interface can have only public members </li></ul><ul><li>A class that is implementing an interface must implement all its members </li></ul><ul><li>A class can implement multiple interfaces </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>

×