Introduction to Programming in  .Net Environment Masoud Milani
Programming in C# Part 2
Overview <ul><li>Classes </li></ul><ul><li>Inheritance </li></ul><ul><li>Abstract classes </li></ul><ul><li>Sealed classes...
Classes <ul><li>Members </li></ul><ul><ul><li>Constants  </li></ul></ul><ul><ul><li>Fields </li></ul></ul><ul><ul><li>Meth...
Parameters Passing <ul><li>Pass by value </li></ul><ul><ul><li>Value types are passed by value by default </li></ul></ul>1...
Parameters Passing <ul><li>Pass by reference </li></ul><ul><ul><li>out  (output) </li></ul></ul>1- <ul><ul><li>©Masoud Mil...
Parameters Passing <ul><li>Pass by reference </li></ul><ul><ul><li>ref  (input and output) </li></ul></ul>1- <ul><ul><li>©...
Parameters Passing <ul><li>Reference types are passed by reference by default </li></ul>1- <ul><ul><li>©Masoud Milani </li...
Variable Number of Parameters 1- <ul><ul><li>©Masoud Milani </li></ul></ul>int a=Smallest(5, 4, -3, 45, 2); int b=Smallest...
Method Overloading <ul><li>More than one method with the same name can be defined for the same class </li></ul><ul><ul><li...
Operator Overloading <ul><li>More than one operator with the same name can be defined for the same class </li></ul><ul><ul...
Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { public MyType(int v) { myValue=v; ...
Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { … public static MyType operator + ...
Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { … public static MyType operator ++...
Operator Overloading 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class MyType { … public static bool operator true...
Indexers <ul><li>Indexers are properties that allow objects to be accessed like arrays </li></ul><ul><ul><li>The accessor ...
Indexer 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Exams { public int this[int examNo] { get { … } set { … ...
Example <ul><li>using System; /// <summary> ///     A simple indexer example. /// </summary> class IntIndexer {     privat...
Example <ul><li>public string this[int pos]     {         get        {             return myData[pos];         }         s...
Example <ul><li>static void Main(string[] args)     {         int size = 10;         IntIndexer myInd = new IntIndexer(siz...
Output <ul><li>Indexer Output  </li></ul><ul><li>myInd[0]: empty  </li></ul><ul><li>myInd[1]: empty  </li></ul><ul><li>myI...
Exercise <ul><li>Write a class Exams that have </li></ul><ul><ul><li>3 private integer fields exam1, exam2 and exam3 </li>...
Exams class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Exams { public Exams(int ex1, int ex2, int ex3) { … ...
Inheritance <ul><li>Implements isA relation </li></ul><ul><ul><li>Student isA Person </li></ul></ul>1- <ul><ul><li>©Masoud...
Inheritance <ul><li>A subclass can hide the inherited members of its superclass using the keyword  new </li></ul>1- <ul><u...
Inheritance 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Person p = new Person(&quot;P1-F&quot;, &quot;P1-L&quot;); p.Show...
Virtual Function <ul><li>A class can allow its subclasses to override its member functions by declaring them  virtual </li...
Virtual Function 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Person p = new Person(&quot;P1-F&quot;, &quot;P1-L&quot;); p...
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 t...
Example <ul><li>using System; </li></ul><ul><li>sealed class MyClass  </li></ul><ul><li>{ </li></ul><ul><li>public int x; ...
Example <ul><li>class MyDerivedC: MyClass  { }  </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>Complation error!
Abstract Classes <ul><li>An abstract class is a class that can not be instantiated </li></ul><ul><ul><li>Abstract classes ...
Abstract Classes <ul><li>Software requirement is given: </li></ul><ul><ul><li>There is no object that is only a person. Ea...
Abstract Classes 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public abstract class Person { public Person(string fn, stri...
Exercise 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Courses exam1 exam2 exam3 Average takes Class Grad Student GradStude...
Exercise <ul><li>Ask the number of students </li></ul><ul><li>For each student ask </li></ul><ul><ul><li>First Name </li><...
Person class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public abstract class Person { public Person(string fn, string l...
Student class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Student : Inheritance.Person { public Student(stri...
GradStudent class 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class GradStudent : Inheritance.Student { public Gra...
Files <ul><li>A file is a sequential sequence of bytes </li></ul><ul><li>Applicable classes </li></ul><ul><ul><li>Text Inp...
StreamWriter 1- <ul><ul><li>©Masoud Milani </li></ul></ul>void WriteText() { int[] data=new int[5]{1,2,3,4,5}; StreamWrite...
StreamReader <ul><li>Similar to StreamWriter </li></ul><ul><ul><li>Check for end of stream using the method  Peek() </li><...
Exercise <ul><li>Modify the previous program to save and retrieve information </li></ul>1- <ul><ul><li>©Masoud Milani </li...
FileStream <ul><li>To open a FileStream, we need to specify </li></ul><ul><ul><li>Physical path </li></ul></ul><ul><ul><li...
FileMode Enumeration <ul><li>Append   </li></ul><ul><ul><li>Opens the file if it exists and seeks to the end of the file, ...
FileAccess Enumeration <ul><li>Read </li></ul><ul><ul><li>Read access  </li></ul></ul><ul><li>ReadWrite </li></ul><ul><ul>...
Formatter <ul><li>Direct reading to and Writing from FileStreams is very difficult </li></ul><ul><ul><li>Must read and wri...
Formatter <ul><li>Methods </li></ul><ul><ul><li>Writing </li></ul></ul><ul><ul><ul><li>Serialize </li></ul></ul></ul><ul><...
Serialization 1- <ul><ul><li>©Masoud Milani </li></ul></ul>string myString=&quot;string1&quot;; int myInt=16; int[] myData...
<ul><ul><li>[Serializable()] </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
Deserialization 1- <ul><ul><li>©Masoud Milani </li></ul></ul>string myString=&quot;string1&quot;; int myInt=16; int[] myDa...
Exercise <ul><li>Modify the previous program to save and retrieve information using a Binary Formatter </li></ul>1- <ul><u...
Exception Handling <ul><li>Exceptions are raised when a program encounters an illegal computation </li></ul><ul><ul><li>Di...
Exception Handling <ul><li>A method that is unable to perform a computation  throws  an exception object </li></ul><ul><li...
Exception Handling <ul><li>try  statement allows for catching exceptions in a block of code </li></ul><ul><li>try statemen...
Exception Handling <ul><li>A try block can have multiple catch clauses </li></ul><ul><ul><li>The catch clauses are searche...
Exception Handling <ul><li>try statement has an optional  finally  clause that executes </li></ul><ul><ul><li>When an exce...
Exception Handling 1- <ul><ul><li>©Masoud Milani </li></ul></ul>int[] a= new int[20]; try { Console.WriteLine(a[2]); } cat...
Exception Handling <ul><li>Exception classes </li></ul><ul><ul><li>IndexOutOfRangeException Class   </li></ul></ul><ul><ul...
Exception Handling <ul><li>Exception classes </li></ul><ul><ul><li>InvalidCastException Class </li></ul></ul><ul><ul><ul><...
Programmer Defined Exceptions <ul><li>Programmer defined exception classes must inherit from  </li></ul><ul><ul><li>Applic...
Programmer Defined Exceptions 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public static int g(int a, int b) { // adds pos...
Delegates <ul><li>A delegate is a class that has only one or more methods </li></ul><ul><ul><li>Allows methods to be treat...
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) ...
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) ...
Exercise <ul><li>Write a sort method that accepts an array of integer and a boolean delegate, compare, and sorts the array...
Interfaces <ul><li>An interface specifies the members that must be provided by classes that implement them </li></ul><ul><...
Interfaces <ul><li>To add an interface, add a class and change it in the editor </li></ul>1- <ul><ul><li>©Masoud Milani </...
Interfaces 1- <ul><ul><li>©Masoud Milani </li></ul></ul>public class Book: IPublication { public Book(string title, string...
Interfaces 1- <ul><ul><li>©Masoud Milani </li></ul></ul>static public void Display(IPublication[] p) { for (int i=0; i<p.L...
Interfaces <ul><li>An interface can have only public members </li></ul><ul><li>A class that is implementing an interface m...
Upcoming SlideShare
Loading in...5
×

C sharp part2

961

Published on

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

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

No notes for slide

C sharp part2

  1. 1. Introduction to Programming in .Net Environment Masoud Milani
  2. 2. Programming in C# Part 2
  3. 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. 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. 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 > b) return b; else return a; } <ul><li>Change in a value parameter is not reflected in the caller </li></ul>
  6. 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>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. 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. 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(&quot;Before Increment, p.myValue={0}&quot;, p.myValue); Increment(p); Console.WriteLine(&quot;After Increment, p.myValue={0}&quot;, p.myValue); static void Increment(ParamClass q) { q.myValue++; } Before Increment, p.myValue=9 After Increment, p.myValue=10
  9. 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 < smallest) smallest=x; return smallest; }
  10. 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. 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. 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. 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. 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. 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(&quot;true&quot;); else Console.WriteLine(&quot;false&quot;);
  16. 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. 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. 18. Example <ul><li>using System; /// <summary> ///     A simple indexer example. /// </summary> class IntIndexer {     private string[] myData;     public IntIndexer(int size)     {         myData = new string[size];         for (int i=0; i < size; i++)         {             myData[i] = &quot;empty&quot;;         }     }     </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  19. 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. 20. Example <ul><li>static void Main(string[] args)     {         int size = 10;         IntIndexer myInd = new IntIndexer(size);         myInd[9] = &quot;Some Value&quot;;         myInd[3] = &quot;Another Value&quot;;         myInd[5] = &quot;Any Value&quot;;         Console.WriteLine(&quot;nIndexer Outputn&quot;);         for (int i=0; i < size; i++)         {             Console.WriteLine(&quot;myInd[{0}]: {1}&quot;, i, myInd[i]);         }     } } </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  21. 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. 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. 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. 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. 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(&quot;Name: {0}, {1}&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(&quot;major: {0}&quot;, major); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  26. 26. Inheritance 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Person p = new Person(&quot;P1-F&quot;, &quot;P1-L&quot;); p.Show(); Console.WriteLine(); Student s=new Student(&quot;S1-F&quot;, &quot;S1-L&quot;, &quot;CS&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. 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(&quot;Name: {0}, {1}&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(&quot;major: {0}&quot;, major); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>using namespace the override keyword is also required.
  28. 28. Virtual Function 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Person p = new Person(&quot;P1-F&quot;, &quot;P1-L&quot;); p.Show(); Console.WriteLine(); Student s=new Student(&quot;S1-F&quot;, &quot;S1-L&quot;, &quot;CS&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. 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. 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(&quot;x = {0}, y = {1}&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. 31. Example <ul><li>class MyDerivedC: MyClass { } </li></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>Complation error!
  32. 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. 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. 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(&quot;Name: {0}, {1}&quot;, lastName, firstName); } } Error: Abstract classes can not be instantiated Person p(“FN-1”, “LN-1”);
  35. 35. Exercise 1- <ul><ul><li>©Masoud Milani </li></ul></ul>Courses exam1 exam2 exam3 Average takes Class Grad Student GradStudent( … ) bool pass // >80 void override Show() class Student string major double Average Student( … ) bool Pass // >60 void override Show() Abstract class Person string fName string lName Person(string fn, string ln) void virtual Show() isA isA
  36. 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. 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. 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. 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. 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. 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(&quot;myOutFile.txt&quot;); for(int i=0; i<data.Length; i++) outFile.WriteLine(&quot;data[{0}]={1}&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. 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(&quot;myOutFile.txt&quot;); while(inpTextFile.Peek() >=0) { string nextLine=inpTextFile.ReadLine(); Console.WriteLine(int.Parse(nextLine.Substring(nextLine.IndexOf(&quot;=&quot;)+1))); } inpTextFile.Close();
  43. 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. 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. 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. 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. 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. 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. 49. Serialization 1- <ul><ul><li>©Masoud Milani </li></ul></ul>string myString=&quot;string1&quot;; int myInt=16; int[] myData=new int[5]{1,2,3,4,5}; FileStream f=new FileStream(&quot;bFile&quot;, FileMode.Create, FileAccess.Write); BinaryFormatter formatter=new BinaryFormatter(); formatter.Serialize(f, myString); formatter.Serialize(f, myInt); formatter.Serialize(f, myData); f.Close();
  50. 50. <ul><ul><li>[Serializable()] </li></ul></ul>1- <ul><ul><li>©Masoud Milani </li></ul></ul>
  51. 51. Deserialization 1- <ul><ul><li>©Masoud Milani </li></ul></ul>string myString=&quot;string1&quot;; int myInt=16; int[] myData=new int[5]{1,2,3,4,5}; f=new FileStream(&quot;bFile&quot;, FileMode.Open, FileAccess.Read); myString=(string)formatter.Deserialize(f); myInt=(int)formatter.Deserialize(f); newdata=(int[])formatter.Deserialize(f); Console.WriteLine(&quot;myString={0}&quot;, myString); Console.WriteLine(&quot;myInt={0}&quot;, myInt);
  52. 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. 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. 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. 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. 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. 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. 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!&quot;); } Finally we are done!
  59. 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. 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. 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. 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 <0) { MyException e=new MyException(&quot;1st parameter is negative&quot;); throw(e); } if (b <0) { MyException e=new MyException(&quot;2nd dparameter is negative&quot;); throw(e); } return a+b; } try { Console.WriteLine(&quot;g(4, -5)={0}&quot;, g(4, -5)); } catch (Exception e) Console.WriteLine(e.Message); }
  63. 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. 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. 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. 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<data.Length; ++i) for(int j=i+1; j<data.Length; j++) if (Compare(data[i],data[j])) { int t=data[i]; data[i]=data[j]; data[j]=t; } }
  67. 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. 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. 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. 70. Interfaces 1- <ul><ul><li>©Masoud Milani </li></ul></ul>static public void Display(IPublication[] p) { for (int i=0; i<p.Length; ++i) p[i].Display(); } static void Main(string[] args) { IPublication[] publications= new IPublication[2]; publications[0] = new Book (&quot;t0&quot;, &quot;a0&quot;, &quot;p0&quot;); publications[1] = new Magazine (&quot;t1&quot;, &quot;a1&quot;); Display(publications); } public class Magazine : Interfaces.IPublication { … }
  71. 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>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×