- Inheritance allows one to create new classes from existing ones, and use said derived instance as if one were using the base instance.
- Derived classes can be used interchangeably. i.e. where a method takes a paramter of a given type, an object of any of its derived types can be passed in.
What is an interface?
- Interface - aka 'contract'.
- Defines a common set of 'members' that all classes that implement said interface, must provide.
- Within the interface declaration, define the members. e.g IDisposable provides the Dispose() mthd, to free up resources. ICloneable - supports copying an object.
- Classes can implement multiple interfaces.
- Visual Studio has a shortcut to extract an interface - Refactor -> Extract Interface.
What are Partial Classes?
- Allows one to split a class definition across multiple source files.
- Benefit - hides detail. Allows derived class writers to focus on that which is significant.
What are Generics?
- Part of the .NF type system
- Enables type-definition while leaving some details unspecified.
- Instead of specifying the types of parameters or member classes, you allow code that uses your type to specify it. Thus consumer code can tailor a type to its own needs.
- .NF 2.0 - System.Collections.Generic namespace - incl. Dictionary, Queue, SortedDictionary, SortedList...
- Above classes have non-generic counterparts in System.Collection - but they offer improved performance and type safety.
Why use Generics?
- In .NF 1.1 - developers used Object class as generic param, and then cast other classes to/fro Object. Generics offer 2 advanatges over this approach:
- 1. reduced runtime errors - compiler cannot catch types errors resulting from casting to/fro Object.
- 2. Improved performance - casting requires 'boxing & unboxing' which slows performance. Generics doesn't require casting or boxing.
How to Create a Generic Type
class Gen
{
public T t;
public U u;
public Gen(T _t, U _u)
{
t = _t;
u = _u;
}
}
- The consuming code will determine the types of T & U.
- Limitation in creating a generic type/class - code valid only if it will compile for every possible constructed instance. Thus you are limited to the capabilities of the base Object class.
How to Consume a Generic Type
- When you consume, you must specify the types for any generics used.
Constraints
- Generics would be v.limited if one could only write code that would compile for any consuming class, because we would then be limited to the capabilities of base Object class.
- Constraints - overcome this limitiation.
- Constraints - place requirements on the types that consuming code can sub for generic.
- 4 types of constraint:
1. Interface - Only allow types that impl. specific interface(s) to use generic.
2. Base class - Only allow types that match/inherit from a specific base class to use gen.
3. Constructor - Requesires types that use gen. to implement a parameterless constructor.
4. Ref. or value type - requires types that use gen. to be either a ref. or value type.
class CompGen
where T : IComparable
{
.
.
.
public T Max()
{
if( t2.CompareTo()t1) <>
return t1;
}
}
Events - what is an event?
- Event - a msg sent by a object to signla the occurance of some action.
- event sender - obj that raises the event.
- event receiver - obj that captures/responds to the event.
What is a Delegate?
- Acts as an intermediary bet event sender class & receiving obj.
- delegate - a class that holds a ref to a mthd.
- delegate class - unlike other classes, it has a signature. And it holds refs to mthds that match its signature.
- delegate decl. - supplies signature of the delegate.
- std signature of an event handler delegate - no ret val., 1st param type Object - refers to instance that raises event. 2nd param derived from EventArgs - holds event data.
- public delegate void AlarmEventHandler(object sender, EventArgs e);
- If event generates data - 2nd param custom type derived from EventArgs.
- associate event with mthd that will handle event - add instance of the delegate to the event.
How to respond to an Event
- 1. Create mthd to respond to event. mthd must match delegate signature.
- 2. Add event handler - to indicate which mthd should receive events.
How to raise an Event
- 1. create a delegate - public delegate void MyEventHandler(object sender, EventArgs e);
- 2. create an event member - public event MyEventHandler MyEvent;
- 3. Invoke the delegate within a mthd when need to raise event:
What are attributes?
- Attributes describe a type, method or property in a way that can be programmatically queried ~ 'reflection'. Use attributes to:
- 1. specify security priveleges for a class - e.g. needing to read a particular file. Runtime will thus throw an exception <>
- Describe an assembly - title, description...
No comments:
Post a Comment