Skip to main content

Constructing Classes



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

Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Concept of App Domain in .Net

Creating Application Domains: Application domain is just like process, provides separate memory space, and isolates from other code. But it’s quite light weight. It also provides the following advantages: 1-       Reliability : If a domain crashes, it can be unloaded. Hence doesn’t affect the other assemblies. 2-       Efficiency : Loading all assemblies in one domain can be cumbersome and can make the process heavy but Appdomains are efficient in this manner. Important properties of AppDomain: ApplicationIdentity , ApplicationTrust , BaseDirectory , CurrentDomain , DomainManager , DomainDirectory , Evidence , FriendlyName , ID , RelativeSearchPath , SetupInformation , ShadowCopyFiles . Important methods of AppDomain: ApplyPolicy , CreateCOMInstanceFrom , CreateDomain , CreateInstance (Assembly). To create an AppDomain: AppDomain adomain = AppDomain.CreateDomain(“D”); To execute an assembly:...

Asynchronous Execution in ASP.NET

Asynchronous Execution: Two ways either implement IHTTPAsyncHandler interface or in ASP.NET 2.0 set <%@ Page Async=”true” %>. The second option implements IHTTPAsyncHandler interface automatically for the page when parsed or compiled. AddOnPreRenderCompleteAsync ( new BeginEventHandler(BeginTask), new EndEventHandler(EndTask)); AddOnPreRenderCompleteAsync() shoud be called in Page_load. The BeginEventHandler and EndEventHandler are delegates defined as follows: IAsyncResult BeginEventHandler( object sender, EventArgs e, AsyncCallback cb, object state) void EndEventHandler( IAsyncResult ar) AsyncProcess starts and completes between PreRender and PreRenderComplete. Other way to perform Async Task is using AsyncPageTask structure. It also allows multiple tasks to execute simultaneously. void Page_Load (object sender, EventArgs e) { PageAsyncTask task = new PageAsyncTask( new BeginEventHandler(BeginTask), new EndEventH...