To Jost .NWDP Site

C#-3 Notes

Review Questions

  1. How do you insure that only one RadioButton web control can be selected in a group of radio buttons?

  2. What does the keyword this mean?

  3. What does this symbol mean?   :

  4. What is inheritance and when should you use it?

  5. What is the is-a test?

  6. Within a constructor, how do you call an overloaded constructor from the same class? a constructor from the base class?

  7. In a class, how do you invoke a method from the base class?

  8. Here are some C# keywords that support inheritance. What do they mean?
  9. In the Employee Example, all of the fields are private within the Person class. How do Employee and Executive objects access the contents of these fields?

  10. In which order do these constructors file: derived class constructor, base class constructor?

  11. True or False. Constructors can be inherited from the base class

  12. Write an ASP.Net example that inputs the make, year, and miles in textboxes, creates a Vehicle object, and adds the vehicle object to an ArrayList collection. Use a Session variable to hold the collection so that it maintains its contents across postbacks.

  13. Look at the Misc.CurrRateFromWeb Example.

 

Examples

Interface1  Vector3  Delegates  RaiseEvent  BoxUnbox  Generic1  Generic2  Generic3  Vector2   Explicit  NewStuff  GarbageCollector  IDisposable  ICloneable  FileIO2  Collections  Generics  BitArray  Indexer  CustomCollection  CustomEnumerator  CustomSerialization  Unsafe
 
Example files: cs3.zip and cs3.txt.

Calendar  CurrRateFromWeb  Chess  VehicleWebpage  EmployeeComboBox1  EmployeeWebPage  BinaryFormatter
 
Example files: misc.zip and misc.htm.

 

Part A: Using the Debugger

  1. Breakpoints   Event.Beverage Example

  2. Step Into, Step Over, Step Out

  3. Value Tooltips

  4. Autos Window

  5. Locals Window

  6. Watch Window

  7. Output Windows

  8. Call Stack

 

Part B: Inheritance

  1. Inheritance in Arrays   C#-2.Virtual Example

  2. Abstract Classes   C#-2.Abstract, C#-2.Brushes Examples

  3. Exception Handling   TryCatch Example

  4. Interfaces   Intro.Multiclass, Intro.Multiproject, Interface1 and Vector3 Examples

 

Part C: Generic Collections

  1. Boxing and Unboxing   BoxUnbox Example

  2. Using Generic Collections   Generic1 Example

  3. Practice Problem

    1. In a Console application, declare and instantiate an ArrayList collection; populate it with five int values.

    2. In a Console application, declare and instantiate a Hashtable collection; populate it with five int values, using these keys: "A", "B", "C", "D", and "E".

    3. Convert the answers of Practice Problems 1 and 2 to ones that use the generic collections List and Dictionary.

 

Part D: Miscellaneous Examples

  1. Calendar   A web page that contains the Calendar and LinkButton web controls.

  2. VehicleWebPage   From Vehicle model fields entered from a web page, create a collection of Vehicle objects, that can be written to disk.

  3. EmployeeComboBox1   A desktop application that displays objects from the Person/Employee/Executive inheritance hierarchy.

  4. EmployeeWebPage   Same as the EmployeeComboBox1 application, but display information on a web page. Complete the application by adding code to add new items.

  5. BinaryFormatter   Show how to serialize a Dictionary collection of Employee objects using the BinaryFormatter class.

 

Part E: More C# Topics

  1. Here is the Operator Precedence Table.

  2. Operator Overloading   Vector2

  3. Checking for Integer Overflow

 

Part F:    Topics Related to Garbage Collection

  1. The GarbageCollector   GarbageCollector Example

  2. IDisposable Interface   IDisposable Example

  3. The IClonable Interface   ICloneable Example

 

Part G:    Unsafe Code

  1. Pointer Types   Unsafe Example

 

Part H:   More about Collections

  1. General Remarks about Collections

    • A collection should have Count and Item properties. It should also have Add and Remove methods.

    • A collection has an Enumerator object, obtained by the GetEnumerator method, for listing the items of the collection sequentially. The Enumerator object has the property Current. It also has the methods Reset and MoveNext.

    • The enumerator is used whenever a foreach loop is invoked.

  2. The Stack, Queue and SortedList Collections   Collections Example

    • A Stack is a LIFO (Last In First Out).

    • Stack methods: Push, Pop, Peek, Clear.

    • A Queue is a FIFO (First In First Out).

    • Queue methods: Enqueue, Dequeue, Peek, Clear.

  3. The Stack<T>, Queue<T> and SortedDictionary<T> Generic Collections   Generics Example

    • Generally, each construction in the Collections example can be literally translated into the constructions in the Generics collection. Only the enumerator for the SortedDictionary collection requires attention.

  4. The BitArray Collection   BitArray Example

    • A BitArray provides efficient storage for a bit array: one bit of memory per bit stored plus overhead. Compare the BitArray Example to the Bitwise Example.

  5. Indexers   Indexer Example

    • An indexer is a method that allows data from a collection to be accessed by index or by key.

    • Here are two sample indexer definitions:

      public typeOfData this[int index]
      { ... }
       
      public typeOfData this[string key]
      { ... }

  6. The IEnumerable and IEnumerator Interfaces   CustomCollection and CustomEnumerator Example

    • If the IEnumerable interface is implemented in a class, its objects can participate in the For..Each construction.

    • If an object implements the IEnumerator interface, the enumerator of the object can be passed on to a collection class containing the object.

  7. Custom Serialization   CustomSerialization Example

    • Classes to be custom serialized must implement the ISerializable interface.

    • Implementing ISerializable requires supplying a GetObjectData method.

    • A special constructor for the object to be serialized must be supplied with this header:
      private ClassName(SerializationInfo si,
          StreamingContext sc);

Part I:   More about File I/O

  1. Using .Net System.IO Classes   C#-2.FileIO1 and Intro.Unicode Examples.

  2. The File Class

  3. The FileInfo, DirectoryInfo and DriveInfo Classes

 

Part J: Delegates and Events

  1. Creating and Using Delegates   Delegates Example

    • A delegate is a type-safe function pointer.

    • Use a delegate in cases where a function is chosen, but invoked at a later time. For example, an event handler is associated with an event, but the event occurs at a later time.

    • Declaring a delegate:
      public delegate ReturnType DelegateType(type2 param2);

    • Declaring Delegate Variable:
      DelegateType delegateVariable;

    • Assigning a method to a delegate (The signatures of the method and delegate must match):
      delegateVariable = methodName;
      or
      delegateVariable = new DelegateType(methodName);

  2. User Defined Events   RaiseEvent Example

    • Declaring an Event:
      public event DelegateType EventName(type1 param1,
          type2 param2);

    • Raising the Event:
      EventName(type1 arg1, type2 arg1);

    • Attaching an Event Handler to an Event:
      objectName.EventName +=
          new DelegateName(EventHandlerName);

    • User defined events are an alternative to exceptions for communicating abnormal situations.

  3. Anonymous Delegates

    • Event handlers can be attached directly to a control's event using an anonymous delegate like this:

      button1.Click += delegate(object source, EventArgs e)
      {
          textBox1.Text = "I\'ve been clicked.";
      };