Sunday, March 30, 2014

Classes in C#

Classes

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. Classes are declared by using the keyword class followed by the class name and a set of class members surrounded by curly braces.
If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable.

Structs

Structs are defined by using the struct keyword, for example:
public struct PostalAddress
{
    // Fields, properties, methods and events go here...
}
Structs share most of the same syntax as classes, although structs are more limited than classes:
·         Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
·         A struct cannot declare a default constructor (a constructor without parameters) or a destructor.
·         Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary.
·         Structs are value types and classes are reference types.
·         Structs can declare constructors that have parameters.
·         A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.
·         A struct can implement interfaces.
·         A struct can be used as a nullable type and can be assigned a null value.
Abstract Classes:
Classes can be declared as abstract by putting the keyword abstract before the class definition. For example:
public abstract class A
{
    // Class members here.
}
An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes
Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method. For example:
public abstract class A
{
    public abstract void DoWork(int i);
}
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. For example:
// compile with: /target:library 
public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}
 
public abstract class E : D
{
    public abstract override void DoWork(int i);
}
 
public class F : E
{
    public override void DoWork(int i)
    {
        // New implementation.
    }
}
If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method—in the previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.
An abstract property declaration does not provide an implementation of the property accessors - it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.
// compile with: csc /target:library abstractshape.cs 
public abstract class Shape
{
    private string name;
 
    public Shape(string s)
    {
        // calling the set accessor of the Id property.
        Id = s;
    }
 
    public string Id
    {
        get
        {
            return name;
        }
 
        set
        {
            name = value;
        }
    }
 
    // Area is a read-only property - only a get accessor is needed: 
    public abstract double Area
    {
        get;
    }
 
    public override string ToString()
    {
        return Id + " Area = " + string.Format("{0:F2}", Area);
    }
}
Modifiers on the property are placed on the property declaration itself. For example: “public abstract double Area”.
 
When declaring an abstract property (such as Area in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a get accessor is available, so the property is read-only.
// compile with: csc /target:library /reference:abstractshape.dll shapes.cs 
public class Square : Shape
{
    private int side;
 
    public Square(int side, string id)
        : base(id)
    {
        this.side = side;
    }
 
    public override double Area
    {
        get
        {
            // Given the side, return the area of a square: 
            return side * side;
        }
    }
}
 
public class Circle : Shape
{
    private int radius;
 
    public Circle(int radius, string id)
        : base(id)
    {
        this.radius = radius;
    }
 
    public override double Area
    {
        get
        {
            // Given the radius, return the area of a circle: 
            return radius * radius * System.Math.PI;
        }
    }
}
 
public class Rectangle : Shape
{
    private int width;
    private int height;
 
    public Rectangle(int width, int height, string id)
        : base(id)
    {
        this.width = width;
        this.height = height;
    }
 
    public override double Area
    {
        get
        {
            // Given the width and height, return the area of a rectangle: 
            return width * height;
        }
    }
}
 
 
Interfaces:
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. In the following example, class ImplementationClass must implement a method named SampleMethod that has no parameters and returns void.
interface ISampleInterface
{
    void SampleMethod();
}
 
class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation:  
    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }
}
1.      An interface can be a member of a namespace or a class and can contain signatures of the following members:
·         Methods
·         Properties
·         Indexers
·         Events
2.      An interface can inherit from one or more base interfaces.
3.      When a base type list contains a base class and interfaces, the base class must come first in the list.
4.      A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.
If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation. In the following example, all the calls to Paint invoke the same method.
class Test 
{
    static void Main()
    {
        SampleClass sc = new SampleClass();
        IControl ctrl = (IControl)sc;
        ISurface srfc = (ISurface)sc;
 
        // The following lines all call the same method.
        sc.Paint();
        ctrl.Paint();
        srfc.Paint();
    }
}
 
 
interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}
class SampleClass : IControl, ISurface
{
    // Both ISurface.Paint and IControl.Paint call this method.  
    public void Paint()
    {
        Console.WriteLine("Paint method in SampleClass");
    }
}
 
// Output: 
// Paint method in SampleClass 
// Paint method in SampleClass 
// Paint method in SampleClass
If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces. It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface. This is accomplished by naming the class member with the name of the interface and a period. For example:
public class SampleClass : IControl, ISurface
{
    void IControl.Paint()
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}
The class member IControl.Paint is only available through the IControl interface, and ISurface.Paint is only available through ISurface. Both method implementations are separate, and neither is available directly on the class
5.      An interface can't contain constants, fields, operators, instance constructors, destructors, or types. Interface members are automatically public, and they can't include any access modifiers. Members also can't be static.
6.      To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.
Difference between Interface & Abstract Class:
Feature Interface Abstract class
Multiple inheritance A class may inherit several interfaces. A class may inherit only one abstract class.
Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers
An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed Requires more time to find the actual method in the corresponding classes. Fast
Adding functionality (Versioning) If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants
No fields can be defined in interfaces
An abstract class can have fields and constrants defined
 
Sealed Classes:
Classes can be declared as sealed by putting the keyword sealed before the class definition. For example:
public sealed class D
{
    // Class members here.
}
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.
A method, indexer, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed. This negates the virtual aspect of the member for any further derived class. For example:
public class D : C
{
    public sealed override void DoWork() { }
}

Static Classes:

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself
The following list provides the main features of a static class:
·         Contains only static members.
·         Cannot be instantiated.
·         Is sealed and therefore cannot be inherited
·         They cannot inherit from any class except Object
·         Cannot contain Instance Constructors.
Static Members:
1.      A non-static class can contain static methods, fields, properties, or events.
2.      The static member is callable on a class even when no instance of the class has been created.
3.      The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.
4.      Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
Members of a class are:
  • Constructors
  • Destructors
  • Fields
  • Methods
  • Properties
  • Indexers
  • Delegates
  • Events
  • Nested Classes


Access Identifiers: All types and type members have an accessibility level, which controls whether they can be used from other code in your assembly or other assemblies. You can use the following access modifiers to specify the accessibility of a type or member when you declare it:

public :The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private : The type or member can be accessed only by code in the same class or struct.

protected :The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal :The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal :The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
 
Constructor: Every class has a constructor, which is called automatically any time an instance of a class is created. The purpose of constructors is to initialize class members when an instance of the class is created. Constructors do not have return values and always have the same name as the class. Listing 7-1 is an example of a class.
Listing 7-1. Example C# Classes:
// Namespace Declaration
using System;

// helper class
class OutputClass
{
    string myString;

    // Constructor
    public OutputClass(string inputString)
    {
        myString = inputString;
    }

    // Instance Method
    public void printString()
    {
        Console.WriteLine("{0}", myString);
    }

    // Destructor
    ~OutputClass()
    {
        // Some resource cleanup routines
    }
}

// Program start class
class ExampleClass
{
    // Main begins program execution.
    public static void Main()
    {
        // Instance of OutputClass
        OutputClass outCl = new OutputClass("This is printed by the output class.");

        // Call Output class' method
        outCl.printString();
    }
}


Constructors are not mandatory, as indicated by the implementation of ExampleClass. In this case, a default constructor is provided. A default constructor is simply a constructor with no arguments.


Default Constructors

If a class does not have a constructor, a default constructor is automatically generated and default values are used to initialize the object fields. A default constructor is simply a constructor with no arguments.

Instance/Parameterized Constructors

Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class. To initialize a static class, or static variables in a non-static class, you must define a static constructor. The following example shows an instance constructor:
class CoOrds
{
    public int x, y;
 
    // constructor 
    public CoOrds()
    {
        x = 0;
        y = 0;
    }
}

Private Constructors

A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. For example:
class NLog
{
    // Private Constructor: 
    private NLog() { }
 
    public static double e = Math.E;  //2.71828...
}
The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you do not use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

Static Constructors

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
class SimpleClass
{
    // Static variable that must be initialized at run time. 
    static readonly long baseline;
 
    // Static constructor is called at most one time, before any 
    // instance constructor is invoked or member is accessed. 
    static SimpleClass()
    {
        baseline = DateTime.Now.Ticks;
    }
}
Static constructors have the following properties:
·         A static constructor does not take access modifiers or have parameters.
·         A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
·         A static constructor cannot be called directly.
·         The user has no control on when the static constructor is executed in the program.
·         A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
·         Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
·         If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

Copy Constructor

C# doesn't provide a copy constructor for objects, but you can write one yourself.
Some important information about constructors:
1.       In the inheritance hierarchy, always the base class constructor is called first. In c#, the base keyword is used to access the base class constructor as shown below.
In the below code we declare a constructor in a derived class. We have used the ':base(...)' keyword after the constructor declaration with a specific parameter list.
classAccount
{
        private string mCode;
        private string mName;
        private string mDescription;
        private double mBalance;
 
        public Account(string code, string name, string description, double balance)
        {
            mCode = code;
            mName = name;
            mDescription = description;
            mBalance = balance;
        }
        public Account()
        {
 
        }
}
 
class PartyAccount :Account
 {
            private string mAddress;
            private string mPhone;
            public PartyAccount(string code, string name, string description, double balance, string address, string phone) : base(code, name, description, balance)
    {
        mAddress = address;
        mPhone = phone;
    }
 
    public PartyAccount() : base()
    {
 
    }
}



Destructors

Destructors are used to destruct instances of classes.
1.       Destructors cannot be defined in structs. They are only used with classes.
2.       A class can only have one destructor.
3.       Destructors cannot be inherited or overloaded.
4.       Destructors cannot be called. They are invoked automatically.
5.       A destructor does not take modifiers or have parameters.
6.       Destructor call from the most-derived to the least-derived.
For example, the following is a declaration of a destructor for the class Car:
class Car
{
    ~Car()  // destructor
    {
        // cleanup statements...
    }
}
The destructor implicitly calls Finalize on the base class of the object. Therefore, the previous destructor code is implicitly translated to the following code:
protected override void Finalize()
{
    try
    {
        // Cleanup statements...
    }
    finally
    {
        base.Finalize();
    }
}

Properties

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
public class Date
{
    private int month = 7;  // Backing store 
 
    public int Month
    {
        get
        {
            return month;
        }
        set
        {
            if ((value > 0) && (value < 13))
            {
                month = value;
            }
        }
    }
}

·         A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels.
·         The value keyword is used to define the value being assigned by the set accessor.
·         Properties that do not implement a set accessor are read only.
·         For simple properties that require no custom accessor code, consider the option of using auto-implemented properties. 
Properties can be declared on an interface. The following is an example of an interface indexer accessor:
public interface ISampleInterface
{
    // Property declaration: 
    string Name
    {
        get;
        set;
    }
}
The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.



Indexers

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.
In the following example, a generic class is defined and provided with simple get and set accessor methods as a means of assigning and retrieving values. The Program class creates an instance of this class for storing strings

class SampleCollection

{

    // Declare an array to store the data elements. 
    private T[] arr = new T[100];
 
    // Define the indexer, which will allow client code 
    // to use [] notation on the class instance itself. 
    // (See line 2 of code in Main below.)         
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets 
            // the corresponding element from the internal array. 
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}
 
// This class shows how client code uses the indexer. 
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();
 
        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
// Output: 
// Hello, World.
1.       Indexers enable objects to be indexed in a similar manner to arrays.
2.       A get accessor returns a value. A set accessor assigns a value.
3.       The this keyword is used to define the indexers.
4.       The value keyword is used to define the value being assigned by the set indexer.
5.       Indexers do not have to be indexed by an integer value; it is up to you how to define the specific look-up mechanism.
6.       Indexers can be overloaded.
7.       Indexers can have more than one formal parameter, for example, when accessing a two-dimensional array.
8.       Indexers can be used in interfaces
 

Saturday, March 29, 2014

ASP.Net Page Life Cycle

When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled, or whether a cached version of the page can be sent in response without running the page. At each stage of the page life cycle, the page raises some events, which could be coded.
Following are the page life cycle events:
PreInit: PreInit is the first event in page life cycle and raised before the initialization stage begins. Use this event for the following: 
  1. Check the IsPostBack property to determine whether this is the first time the page is being processed. If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.
  2. The IsCallback and IsCrossPagePostBack properties have also been set at this time.
  3. Create or re-create dynamic controls.
  4. Set a master page & theme property dynamically.
  5. Read or set profile property values.
This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.
Master page doesn't have PreInit method because Master pages are derived from Control class as seen in below hierarchy:
Init: This event is raised after all controls have been initialized and any skin settings have been applied. Init event initializes the control property and the control tree is built. The Init event of individual controls occurs before the Init event of the page. Use this event to read or initialize control properties.

This event can be handled by overloading the OnInit method or creating a Page_Init handler.

Order:
1.       Master page controls Init event
2.       Content controls Init event.
3.       Master page Init event.
4.       Content page Init event.
 
InitComplete: This event is raised at the end of the page's initialization stage. InitComplete event allows tracking of view state. All the controls turn on view-state tracking.
View state tracking enables controls to persist any values that are programmatically added to the ViewState collection. Use this event to make changes to view state that you want to make sure are persisted after the next postback.

 


PreLoad: This event is raised after the page loads view state for itself and all controls & occurs before the post back data is loaded in the controls

This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.

Master page doesn't have PreLoad method.


Load: The Load event is raised for the page first and then recursively does the same for each child control until the page and all controls are loaded.
This event can be handled by overloading the OnLoad method or creating a Page_Load handler.
Order:
1.       Content page Load event.
2.       Master page Load event.
3.       Master page controls Load event.
4.       Content page controls Load event.
 
Load Complete: This event is raised at the end of the event-handling stage. Use this event for tasks that require that all other controls on the page be loaded.

This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.

Master page doesn't have OnLoadComplete method.
 

PreRender: This event is raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls. The Page object raises the PreRender event on the Page, and then recursively does the same for each child control. Use the event to make final changes to the contents of the page or its controls before the rendering stage begins.

In this event, Page ensures that all child controls are created. Page calls EnsureChildControls for all controls, including itself. Every control whose datasource/databind property is set calls for its databind method.

Order:
5.       Content page PreRender event.
6.       Master page PreRender event.
7.       Master page controls PreRender event.
8.       Content page controls PreRender event.
 
PreRenderComplete: This event is raised after each control's PreRender property is completed. This event ensures the completion of the pre-rendering phase.

SaveStateComplete: This event is raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, but the changes will not be persist on the next postback.


Render: Every ASP.NET control has render method and the page instance calls this method to output the control’s markup, after this event any changes to the page or controls are ignored.

If you create a custom control, you typically override this method to output the control's markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method.

A user control automatically incorporates rendering, so you do not need to explicitly render the control in code.
 
Unload: This event is raised for each control and then for the page. the UnLoad phase is the last phase of the page life cycle. During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream. If you attempt to call a method such as the Response.Write method, the page will throw an exception.

In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.

For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.
The following is the sequence in which events occur when a master page is merged with a content page:
1.       Content page PreInit event.
2.       Master page controls Init event.
3.       Content controls Init event.
4.       Master page Init event.
5.       Content page Init event.
6.       Content page PreLoad event
7.       Content page Load event.
8.       Master page Load event.
9.       Master page controls Load event.
10.   Content page controls Load event.
11.   Content Page LoadComplete event.
12.   Content page PreRender event.
13.   Master page PreRender event.
14.   Master page controls PreRender event.
15.   Content page controls PreRender event.
16.   Master page controls Unload event.
17.   Content page controls Unload event.
18.   Master page Unload event.
19.   Content page Unload event.