Wednesday, April 27, 2011

What is Overloading and Overriding

Overloading - is the concept of using one function or class in different ways by changing the signature of its parameters. We can define a function with multiple signatures without using the keyword Overloads, but if you use the Overloads keyword in one, you must use it in all of the function's Overloaded signatures.

The Overloads keyword is used in VB.NET, while the Overload keyword is used in C# (There is no other difference). The Overloads property allows a function to be described using deferent combinations of parameters. Each combination is considered a signature, thereby uniquely defining an instance of the method being defined.

Overloading is a way through which polymorphism is achieved.

Overloading

Overloading is when you have multiple methods in the same scope, with the same name but different signatures.

//Overloading

public class test

{

public void getStuff(int id)

{}

public void getStuff(string name)

{}

}

Overriding

Overriding is a principle that allows you to change the functionality of a method in a child class.

//Overriding

public class test

{

public virtual getStuff(int id)

{

//Get stuff default location

}

}



public class test2 : test

{

public override getStuff(int id)

{

//base.getStuff(id);

//or - Get stuff new location

}

}

Tuesday, April 26, 2011

Optional Parameters in C#

C# does not have an implementation of optional parameters like those found in php, however you can simulate this feature using method overloading.

static int Add(int a, int b)
{
return Add(a, b, 0, 0);
}
 
static int Add(int a, int b, int c)
{
  return Add(a, b, c, 0);
}
 
static int Add(int a, int b, int c, int d)
{
  return (a + b + c + d);
}

What is the virtual keyword used for?

Virtual - If a base class method is to be overriden, it is defined using the keyword virtual (otherwise the sealed keyword is used to prevent overriding).
Note that the class member method may be overriden even if the virtual keyword is not used, but its usage makes the code more transparent & meaningful. In VB.NET, we may use the Overridable keyword for this purpose.

When the override keyword is used to override the virtual method, in a scenario where the base class method is required in a child class along with the overriden method, then the base keyword may be used to access the parent class member. The following code example will make the usage more clear.

public class Employee
{
public virtual void myfunction(float money) //This method may be overriden
{ Basic += money; }
}


public class Manager : Employee
{
public override void myfunction(float money) //This method is being overriden
{
float incentive = 10000;
base.SetSalary(money + incentive); //Calling base class method
}
}

Event sequence for Mastre Page-Content Page-User Control

Scenario 1:

Master page - Content page

Sequence of events

Content Page PreInit Event

Master Page Init Event

Content Page Init Event

Content Page Load Event

Master Page Load Event

If we are using master page and want to changes theme or decide which master page need to be load at runtime then we need to use Content page PreInit event.

Scenario 2:

Master page - Content page - Web user control on master page only.

Content Page PreInit Event

Master: UserControl Init Event

Master Page Init Event

Content Page Init Event

Content Page Load Event

Master Page Load Event

Master: UserControl Load Event

Scenario 3:

Master page - web user control on master page - Content page - Web user control on master page only.

Content Page PreInit Event

Content Page UserControl Init Event

Master:UserControl Init Event

Master Page Init Event

Content Page Init Event

Content Page Load Event

Master Page Load Event

Page:UserControl Load Event

Master:UserControl Load Event

PreRender - Page
PreRender - MasterPage
PreRender - UserControl

Unload - ChildUserControl
Unload - UserControl
Unload - MasterPage
Unload - Page

Conclusion :

1) PreInit for Content Page is first event

2) Init event sequence: UsercontrolàMaster PageàContent Page

3) Load event sequence: Content Page àMaster Pageà Usercontrol

4) Prerender event sequence is same as Load

5) Unload is exact reverse of load. Same as Init

UsercontrolàMaster PageàContent Page

6) Init and unload are same sequence

7) Load and prerender has same sequence

8) Webcontrol on content page has precedence over usercontrol in master page