Inheritance is about defining a class
in-term of another in a more specialized way. For example Bitmap Class is
derived from Image classes. Bitmap adds few new feature and facilities and uses
a lot of the features of Image classes. Its consistency in coding and prevents
from recoding the same thing.
Interface is contract that defines a
common set of members and functionalities which all classes that inherit from
it must provide.
Commonly used interfaces:
IComparable Required for sorting
IDisposable To dispose of the
object related resources immediately
IConvertable Enables a class to be
converted to Boolean, Byte, Double, or String
ICloneable Copying an Object
IEquatable enables if (a==b)
IFormattable Convert the value of an object to a specialized
formatted string.
Partial classes allow you to split the
definition of class across multiple files.
Generics allow you to define a class
without specifying data type for one or more data members, and their data type
will be determined when their instance will be created.
.Net Framework also provides some collections in Generic
format in System.Collections.Generics.
Generics Constrains:
Generics Support six types of Constrains which are:
|
Constraint
|
Description
|
|
where T: struct |
The type argument must be a value type. Any value type except
Nullable can be specified. |
|
where T : class |
The type argument must be a reference type, including any class,
interface, delegate, or array type. |
|
where T : new() |
The type argument must have a public parameter less constructor.
When used in conjunction with other constraints, the new() constraint
must be specified last. |
|
where T : <base class name> |
The type argument must be or derive from the specified base
class. |
|
where T : <interface name> |
The type argument must be or implement the specified interface.
Multiple interface constraints can be specified. The constraining interface
can also be generic. |
|
where T : U |
The type argument supplied for T must be or derive from the
argument supplied for U. This is called a naked type constraint. |
Example:
class EmployeeList<T> where T : Employee, IEmployee,
System.IComparable<T>, new()
Unbounded Type Parameters
Type parameters that have no constraints, such as T in public
class SampleClass<T>{}, are called unbounded type parameters. Unbounded
type parameters have the following rules:
The != and == operators cannot be used because there is no
guarantee that the concrete type argument will support these operators.
They can be converted to and from System.Object or explicitly
converted to any interface type.
You can compare to null. If an unbounded parameter is
compared to null, the comparison will always return false if the type argument
is a value type.
Use the default keyword, which
will return null for reference types and zero for numeric value types. For
structs, it will return each member of the struct initialized to zero or null
depending on whether they are value or reference types.
public class
GenericList<T>
{
public GenericList<T>()
{
mNode = default(T);
}
}
Event is a message sent by an object
to another object to signal the occurrence of an action.
Delegate is a class that can hold
reference to a method.
Public delegate void
alaramEvent(object sender , EventArgs e)
Attributes provide a powerful method
of associating declarative information with C# code (types, methods,
properties, and so forth).
Type forwarding allows you to move a
type from one assembly (assembly A) into another assembly (assembly B), such
that, it is not necessary to recompile clients that consume assembly A.
Comments