Saturday, November 26, 2011

STATIC Keyword in depth(C#)

The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.

The static modifier on a class means that the class cannot be instantiated, and that all of its members are static.

The following class is declared as static and contains only static methods:
static class CompanyEmployee
{
public static void DoSomething() { /*...*/ }
public static void DoSomethingElse() { /*...*/ }
}

A constant or type declaration is implicitly a static member.

A static member cannot be referenced through an instance. Instead, it is referenced through the type name.

For example, consider the following class:
public class MyBaseC
{
public struct MyStruct
{
public static int x = 100;
}
}
To refer to the static member x, use the fully qualified name (unless it is accessible from the same scope):
MyBaseC.MyStruct.x

*You can directly access structs and static memebers without creating the instance of class.

While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.

It is not possible to use this to reference static methods or property accessors.

If the static keyword is applied to a class, all the members of the class must be static. Otherwise a complie time error will generate stating that you can not declare instance member inside a static class.

Classes and static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated.

STATIC CLASS:
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.
For example, if you have a static class that is named UtilityClass that has a public method named MethodA, you call the method as shown in the following example:
UtilityClass.MethodA();

static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.

The following list provides the main features of a static class:
Contains only static members.
Cannot be instantiated.
Is sealed.
Cannot contain Instance Constructors.

Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor; however, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.

STATIC MEMBERS:

A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. 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. 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.

Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.

Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.

Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object instance is required.

C# does not support static local variables (variables that are declared in method scope).
You declare static class members by using the static keyword before the return type of the member, as shown in the following example:

Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called.

If your class contains static fields, provide a static constructor that initializes them when the class is loaded.

Static Constructors (C# Programming Guide)
A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Static constructors have the following properties:
A static constructor does not take access modifiers or have parameters.
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.
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.

Monday, November 7, 2011

CLR Architecture

Introduction

Common Language Runtime (CLR) is platform dependent, where as Intermediate Langage (IL) code is platform independent. When dotnet application is executed the request will goto operating system. Operating system will create memory process, and IL instructions will be loaded. Operating system will load CLR to process IL instructions.




The program used for calling CLR is called 'Runtime Host'. Runtime Host for desktop application is windows API function-"corbindtoruntime". Runtime Host for web based application is "Asp.Net worker process".

CLR is a runtime engine with collection of services:

'Assembly Resolver' will read manifest of exe file, it will identify private assemblies and shared assemblies required for application execution. It will forward request to 'Assembly Loader'

Assembly loader will load assemblies from application folder or GAC into application process. 'Type Checker' will verify types used by application with CLS or CTS standards. This provides Type Safety.

'Debug Manager' will activate debugger utility for line by line execution. It allows modification of code without terminating application.

'Exception Manager' will handle exceptions thrown by application. If there is no catch block (that is solution from developer), the application will be terminated.

'Com Marshals' will provide communication with Com component from dotnet application. This provides COM Interoperatability.

'Thread Support' service wil manage multiple threads with in application process. This provides Multi-Threading.

'Garbage Collector' will release unused objects memory. This provides automatic memory management

IL to native compiler is called JIT (Just in Time) compiler. This will process IL instructions into operating system native code.

Saturday, November 5, 2011

Polymorphism in OOPS

Polymorphism

What is polymorphism?
The meaning of the word polymorphism is something like one name, many forms.
How does C# implement polymorphism?
Defination:providing many functionalities with a single insatance name
Polymorphism manifests itself in C# in the form of multiple methods having the same name.
From a practical programming viewpoint, polymorphism manifests itself in two distinct forms in C#:


Fucntion Overloading
Function Overriding



Fucntion Overloading:


Ø Overloading means having more than one method with the same name, but a different number(list) of arguments or diiferent type of arguments in the same class or in a combination of base and derived classes .
Example:
class Projector
{
public static void Display( string str )
{
System.Console.WriteLine( str );
}
public static void Display( int number )
{
System.Console.WriteLine( number);
}

public static void Main()
{
Projector.Display( "Hello" );
Projector.Display( 1234 );

}
}

Output:
Hello
1234

Examples.for Method Overloading possibilities:
1)void calaculate();
2)void calaculate(int a); //ok:u can overload
3)void calaculate(string a, int b);//ok u can overload
4)void calaculate(string h, int t);//error 3 and 4 can’t overload,because signatures are same
5)int calaculate(int g); //error:2 and 5 can't overload based on return type only
//ref method
6)void calaulate(ref int x);// u can overload with all the above methods but u can’t overload with 7th method.
// out method
7)void calaculate(out int x);// u can not overload 6th and 7th methods,but with other methods u can overload 6th and 7th methods individually.


Function Overriding



2)Function Overriding:
Overriding means have a method with the same name and the same signature in the base class and derived classes.
To override a method we have to use “virtual” keyword for base class method and override keyword for derived class method.

Example:
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public override void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 ;
d1= new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
When we declare a virtual method, it must contain a method body. Other wise the compiler will generate an error.
Remember that, since virtual methods are used for achieving polymorphism and since polymorphism works only with objects, it not possible to declare a static method as virtual in C#.
Similarly the private methods are also not possible to declare virtual, since they can’t override inside a derived class.

Method overriding is a feature that allows you to invoke functions (that have the same signatures) that belong to different classes in the same hierarchy of inheritance using the base class reference..

Both methods should have the same access level; the same return type, the same name and same method signature.








Let's understand this through small examples.
Example for NEW keyword:
We can override a method without declaring the base class method as 'virtual'. We can use the modifier 'new' to tell the compiler that the derived class method 'hides' the base class method.

class Base
{
public void Display()
{
Console.WriteLine("Base method");
}
}

class Derived:Base
{
new public void Display()
{
Console.WriteLine("Drived method");
}
}

class HideTest
{
public static void Main()
{

Base b=new Base();
b.display();
Derived d=new Derived();
d.Display();
}
}
Output:
Base method
Derived method
Example1:

class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class Demo
{
public static void Main()
{
BC b; // creating reference variable b of type BC
b = new BC(); //creating object of type BC and assign its reference to reference variable b

b.Display();
}
}
Output
BC::Display
The above program compiles and runs successfully to give the desired output. It consists of a base class BC and a derived class DC. Class BC consists of function Display(). Class DC hides the function Display() it inherited from the base class BC by providing its on implementatin of Display(). Class Demo consists of entrypoint function Main(). Inside Main() we first create a reference b of type BC. Then we create an object of type BC and assign its reference to reference variable b. Using the reference variable b we invoke the function Display(). As expected, Display() of class BC is executed because the reference variable b refers to the object of class BC.
Now we add a twist of line to the above program.

Example 2

class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();
}
}
Output




BC::Display
BC::Display
Here we are creating an object of Derived class DC and storing its reference in the reference variable b of type BC. This is valid in C#. Next, using the reference variable b we invoke the function Display(). Since b contains a reference to object of type DC one would expect the function Display() of class DC to get executed. But that does not happen. Instead what is executed is the Display() of BC class. That's because the function is invoked based on type of the reference and not to what the reference variable b refers to. Since b is a reference of type BC, the function Display() of class BC will be invoked, no matter whom b refers to. Take one more example.
Example 3

class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : BC
{
new public void Display()
{
System.Console.WriteLine("TC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();

b = new TC();
b.Display();
}
}
Output

BC::Display
BC::Display
BC::Display
The output of the above program is a receipt of the fact that no matter to whom base class reference b refers, it invokes the functions of the class that matches its type.
But actually , If b contains the reference to a particular derived class object, then its supposed to invoke the function of that class.
Well, C# helps us do this by the usage of keywords virtual and override as shown in the following program.
Example 4

class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();
}
}
Output
BC::Display
DC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the Derived class's implementation of Display() is decorated with the modifier override. Doing so enables C# to invoke functions like Display() based on objects the reference variable refers to and not the type of reference that is invoking the function. Hence in the above program when b refers to the object of class BC it invokes Display() of BC and then when b refers to the object of class DC it invokes Display() of class DC. Let's see if this holds true for the third generation of derived classes. Take the following program.
Example 5
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : DC
{
public override void Display()
{
System.Console.WriteLine("TC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();

b = new TC();
b.Display();
}
}
Output
BC::Display
DC::Display
TC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the implementation of Display() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable b and invoke Display(). The rite versions of Display get invoked based on the object the reference variable refers to. Time for a tiny teaser! Guess what the output would be in the following program?
Example 6
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : DC
{

}

class Demo
{
public static void Main()
{
BC b;

b = new TC();
b.Display();
}
}
Output
DC::Display
Since TC has no implementation of Display(), it inherits Display() from DC as TC is derived from DC. Hence Display() from Derived class DC gets executed. It's as if the derived class TC looked like this:
class TC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
to the compiler. Take one more example. Guess what its output will be.

Example 7

class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : DC
{
public new void Display()
{
System.Console.WriteLine("TC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;

b = new TC();
b.Display();
}
}
Output

DC::Display
Agreed that TC defines its own new version of Display(). But its version of display is not invoked as Display() of TC does not override the Display() of the base class. With this understood we are done with Method overriding in C#.

Differences between Function overriding and Function overloading


Function Overloading Function Overriding
1) Both method names should be same,but signature should not be same.
1) Both method names,and signature and retun type should be same

2) In Overloading we will not use any virtual and override keywords 2) In overriding we have to use Virtual and Override keywords
3)Overloading we can implement with in single class or combination
of base and derived classes.

3)We can implement Overriding in base class and derived class but we can not implement in a single class

Understanding OOPs concepts

Object Oriented Programming or OOPs is a widely used term in Computer Science . OOPs is a vital concept for every Computer Science students , it is asked not only in exams but also in jobs interviews as well .Many students find it difficult to understand these Object Oriented Programming (OOP) concepts . So I decided to write an article on it , I hope you will like it .

There are many terms in Object Oriented programing , together these terms are called as OOP s concepts . These OOP s concepts are used in all modern programming languages like Java , J2EE,C++ , C#, .Net , Python and Javascript .Lets us begin with these OOPs concepts.

Class

As per C# .net class is a collection fields,properties, method and events,Object is instance of a class.
A C# Class is considered to be the primary building block of the language. What I mean by the primary building block is that every time you work with C# you will create classes to form a program. We use classes as a template to put the properties and functionality or behaviors in one building block for a group of objects and after that we use the template to create the objects we need.



Objects


Objects can be considered as a thing that performs a set of related functions .Programming objects are used to model real worlds objects. An object is also an instant of a class . For our class Car , Ferrari will be our object




Inheritance



Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective employment of reuse, you can save time in your programming.

Definition :-Deriving the features from one class into another class is called inheritance.
Example1 for implementing Inheritence:

Inheritance is one of the primary concepts of object-oriented programming. It allows you to reuse existing code. Through effective employment of reuse, you can save time in your programming.

Definition :-Deriving the features from one class into another class is called inheritance.
Example1 for implementing Inheritence:
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}

public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}

public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}

public static void Main()
{
ChildClass child = new ChildClass();

child.print();
}
}

Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Example2:
// Inheritance

using System;
class Base
{
public int x = 10;
public int y = 20;
public void Method()
{
Console.WriteLine("Base Method");
}
}
class Derived : Base
{
public int z = 30;
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
Console.WriteLine("{0},{1},{2}",d1.x,d1.y,d1.z); // displays 10,20,30
d1.Method();// displays 'Base Method'
}
}



Note: C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance.

Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Example2:
// Inheritance

using System;
class Base
{
public int x = 10;
public int y = 20;
public void Method()
{
Console.WriteLine("Base Method");
}
}
class Derived : Base
{
public int z = 30;
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
Console.WriteLine("{0},{1},{2}",d1.x,d1.y,d1.z); // displays 10,20,30
d1.Method();// displays 'Base Method'
}
}


Note: C# supports single class inheritance only. Therefore, you can specify only one base class to inherit from. However, it does allow multiple interface inheritance.


Interfaces:



An Interface is a collection of abstract members
An interface looks like a class, but has no implementation.
An interface has no implementation; it only has the signature or in other words, just the declaraton of the methods without the body
An Interface is a reference type and it contains only abstract members
All the member declarations inside interface are implicitly public
Interface can contain declaration of events, indexers, methods and/or properties. They have no body, just the declaration.
The interface can't contain constants, data fields, constructors, destructors and static members
Interface cannot be instantiated


Example:
interface IMyInterface
{
void MethodToImplement();
}

defines an interface named IMyInterface. A common naming convention is to prefix all interface names with a capital "I". This interface has a single method named MethodToImplement(). This could have been any type of method declaration with different parameters and return types. I just chose to declare this method with no parameters and a void return type to make the example easy. Notice that this method does not have an implementation (instructions between curly braces - {}), but instead ends with a semi-colon, ";". This is because the interface only specifies the signature of methods that an inheriting class or struct must implement.
Example:. Using an Interface implementing Inheritance:
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}

public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}

output=MethidToImplement() called
Example2:
using System;

interface IParentInterface
{
void ParentInterfaceMethod();
}

interface IMyInterface : IParentInterface
{
void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
iImp.ParentInterfaceMethod();
}
public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}

public void ParentInterfaceMethod()
{
Console.WriteLine("ParentInterfaceMethod() called.");
}
}

output = MethodToImplement() called
ParentInterfaceMethod() called

The above example contains two interfaces: IMyInterface and the interface it inherits, IParentInterface. When one interface inherits another, any implementing class or struct must implement every interface member in the entire inheritance chain. Since the InterfaceImplementer class inherits from IMyInterface, it also inherits IParentInterface. Therefore, the InterfaceImplementer class must implement the MethodToImplement() method specified in the IMyInterface interface and the ParentInterfaceMethod() method specified in the IParentInterface interface.
Example 2 for Interface
using System;
using System.Collections.Generic;
using System.Text;

namespace Interfaces1
{


interface ICollegeInterface
{
void CollegeInterfaceMethod();
}

interface IStudentInterface : ICollegeInterface
{
void StudentInterfaceMethod();
}
class UniversityClass : IStudentInterface
{
static void Main(string[] args)
{
UniversityClass uni= new UniversityClass();
uni.CollegeInterfaceMethod();
uni.StudentInterfaceMethod();


Console.ReadLine();

}



public void CollegeInterfaceMethod()
{
Console.WriteLine("CollegeInterfaceMethod() called.");
}
public void StudentInterfaceMethod()
{
Console.WriteLine("StudentInterfaceMethod() called.");
}



}
}








Abstraction


Abstraction defines way to abstract or hide your data and members from outside world. Simply speaking Abstraction is hiding the complexities of your class or struct or in a generic term Type from outer world. This is achieved by means of access specifiers.

Definition: - Hiding the complexities of your type from outside world.
How to Abstract: - By using Access Specifiers
.Net has five access specifiers:-Public,private,prtotected,internal and protected internal.

Example1 for Abstraction and Encapsulation implimentation:
public class Class1
{

private int i; // private
public int j; // Public
protected int k; //Protected data
internal int m; // Internal means visible inside assembly
protected internal int n; //inside assembly as well as to derived classes outside assembly
public static int y; //Static

public void myFoo()
{
//Within a class if you create an object of same class then you can access all data members through object reference even private data too
Class1 obj = new Class1();
obj.i =10; //Error can’t access private data through object.But here it is accessible.:)
obj.j =10;
obj.k=10;
obj.m=10;
obj.n=10;

// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
}


Now lets try to write the same code inside Main method and try to compile

static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data //through object.
obj.j =10;
// //obj.k=10; //Error can’t access protected data through object.
obj.m=10;
obj.n=10;
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}

What if Main is inside another assembly

static void Main()
{
//Access specifiers comes into picture only when you create object of class outside the class
Class1 obj = new Class1();
// obj.i =10; //Error can’t access private data through object.
obj.j =10;
// obj.k=10; //Error can’t access protected data through object.
// obj.m=10; // Error can’t access internal data outside assembly
// obj.n=10; // Error can’t access protected internal data outside assembly
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}







Encapsulation

Encapsulation is process of keeping data and methods together inside objects
Definition:- Binding data and member functions together inside a single unit.
How to Encapsulate:- By creating types e.g Classes and Struct






Polymorphism


What is polymorphism?
The meaning of the word polymorphism is something like one name, many forms.
How does C# implement polymorphism?
Defination:providing many functionalities with a single insatance name
Polymorphism manifests itself in C# in the form of multiple methods having the same name.
From a practical programming viewpoint, polymorphism manifests itself in two distinct forms in C#:
Fucntion Overloading:
Ø Overloading means having more than one method with the same name, but a different number(list) of arguments or diiferent type of arguments in the same class or in a combination of base and derived classes .
Example:
class Projector
{
public static void Display( string str )
{
System.Console.WriteLine( str );
}
public static void Display( int number )
{
System.Console.WriteLine( number);
}

public static void Main()
{
Projector.Display( "Hello" );
Projector.Display( 1234 );

}
}

Output:
Hello
1234

Function Overloading:-

Function overloading depends on:-

1.) Variable Number of parameters to function

2.) Order of parameters to function

3.) Data type of parameters

First we will consider cases on which function overloading depends on
Variable Number of parameters to function
Example 1:
public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public void foo(int i)
{
Console.WriteLine("Hi I am overloaded and here is the value of i " + i);
}
}

In this example, number of parameters are different so this will compile fine and now when you create object of this class you will see two overloaded version of foo.
Lets now create object of this class and call both the versions:-

MyBaseClass obj = new MyBaseClass();
obj.foo();
obj.foo(10);



output:-

Hi I am original
Hi I am overloaded and here is the value of i 10


2) Order of parameters to function:

public class MyBaseClass
{
public void foo(int i , string s)
{
Console.WriteLine("The parameter order is int and string.The value of i is {0} and that of s is {1}",i,s);
}
public void foo(string s, int i)
{
Console.WriteLine("The parameter order is string and int.The value of s is {0} and that of i is {1}", s, i);
}
}

In the example above, number of parameters are same but since the order is different so this will compile with out errors.

Lets now create object of this class and call both the versions:-

MyBaseClass obj = new MyBaseClass();
obj.foo(1,”shakti”);
obj.foo(“Test”,2);


output:-

The parameter order is int and string.The value of i is 1 and that of s is shakti
The parameter order is string and int.The value of s is Test and that of i is 2
3)Data type of parameters:

public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("This is one parameter function which takes int as parameter and value of i is {0}.",i);
}
public void foo(string s )
{
Console.WriteLine("This is one parameter function which takes string as parameter and value of s is {0}.", s);
}
}

Here the number of parameters are same but data types are different so this will compile fine.

Lets now create object of this class and call both the versions:-

MyBaseClass obj = new MyBaseClass();
obj.foo(10);
obj.foo("Test");


Output:-

This is one parameter function which takes int as parameter and value of i is 10.

This is one parameter function which takes string as parameter and value of s is Test.

Function overloading doesn’t depend on:-

1.) Return type of function

2.) Name of parameters
Now we will consider cases on which function overloading does not depend on:-
1.) Return type of function:

public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public int foo()
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}


The code sample will generate the following error:-

Type ‘MyBaseClass' already defines a member called 'foo' with the same parameter types

2.) Name of parameters:


public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("Hi I am original");
}
public int foo(int j)
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}
The above code will give the following error:-

Type 'MyBaseClass' already defines a member called 'foo' with the same parameter types.


Examples.for Method Overloading possibilities:
1)void calaculate();
2)void calaculate(int a); //ok:u can overload
3)void calaculate(string a, int b);//ok u can overload
4)void calaculate(string h, int t);//error 3 and 4 can’t overload,because signatures are same
5)int calaculate(int g); //error:2 and 5 can't overload based on return type only
//ref method
6)void calaulate(ref int x);// u can overload with all the above methods but u can’t overload with 7th method.
// out method
7)void calaculate(out int x);// u can not overload 6th and 7th methods,but with other methods u can overload 6th and 7th methods individually.











2)Function Overriding:
Overriding means have a method with the same name and the same signature in the base class and derived classes.
To override a method we have to use “virtual” keyword for base class method and override keyword for derived class method.

Example:
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public override void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 ;
d1= new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
When we declare a virtual method, it must contain a method body. Other wise the compiler will generate an error.
Remember that, since virtual methods are used for achieving polymorphism and since polymorphism works only with objects, it not possible to declare a static method as virtual in C#.
Similarly the private methods are also not possible to declare virtual, since they can’t override inside a derived class.

Method overriding is a feature that allows you to invoke functions (that have the same signatures) that belong to different classes in the same hierarchy of inheritance using the base class reference..

Both methods should have the same access level; the same return type, the same name and same method signature.








Let's understand this through small examples.
Example for NEW keyword:
We can override a method without declaring the base class method as 'virtual'. We can use the modifier 'new' to tell the compiler that the derived class method 'hides' the base class method.

class Base
{
public void Display()
{
Console.WriteLine("Base method");
}
}

class Derived:Base
{
new public void Display()
{
Console.WriteLine("Drived method");
}
}

class HideTest
{
public static void Main()
{

Base b=new Base();
b.display();
Derived d=new Derived();
d.Display();
}
}
Output:
Base method
Derived method
Example1:

class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class Demo
{
public static void Main()
{
BC b; // creating reference variable b of type BC
b = new BC(); //creating object of type BC and assign its reference to reference variable b

b.Display();
}
}
Output
BC::Display
The above program compiles and runs successfully to give the desired output. It consists of a base class BC and a derived class DC. Class BC consists of function Display(). Class DC hides the function Display() it inherited from the base class BC by providing its on implementatin of Display(). Class Demo consists of entrypoint function Main(). Inside Main() we first create a reference b of type BC. Then we create an object of type BC and assign its reference to reference variable b. Using the reference variable b we invoke the function Display(). As expected, Display() of class BC is executed because the reference variable b refers to the object of class BC.
Now we add a twist of line to the above program.

Example 2

class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();
}
}
Output




BC::Display
BC::Display
Here we are creating an object of Derived class DC and storing its reference in the reference variable b of type BC. This is valid in C#. Next, using the reference variable b we invoke the function Display(). Since b contains a reference to object of type DC one would expect the function Display() of class DC to get executed. But that does not happen. Instead what is executed is the Display() of BC class. That's because the function is invoked based on type of the reference and not to what the reference variable b refers to. Since b is a reference of type BC, the function Display() of class BC will be invoked, no matter whom b refers to. Take one more example.
Example 3

class BC
{
public void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
new public void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : BC
{
new public void Display()
{
System.Console.WriteLine("TC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();

b = new TC();
b.Display();
}
}
Output

BC::Display
BC::Display
BC::Display
The output of the above program is a receipt of the fact that no matter to whom base class reference b refers, it invokes the functions of the class that matches its type.
But actually , If b contains the reference to a particular derived class object, then its supposed to invoke the function of that class.
Well, C# helps us do this by the usage of keywords virtual and override as shown in the following program.
Example 4

class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();
}
}
Output
BC::Display
DC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the Derived class's implementation of Display() is decorated with the modifier override. Doing so enables C# to invoke functions like Display() based on objects the reference variable refers to and not the type of reference that is invoking the function. Hence in the above program when b refers to the object of class BC it invokes Display() of BC and then when b refers to the object of class DC it invokes Display() of class DC. Let's see if this holds true for the third generation of derived classes. Take the following program.
Example 5
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : DC
{
public override void Display()
{
System.Console.WriteLine("TC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;
b = new BC();
b.Display();

b = new DC();
b.Display();

b = new TC();
b.Display();
}
}
Output
BC::Display
DC::Display
TC::Display
The above program compiles and runs successfully to give the expected desired output. The function Display() of Base class BC is declared as virtual, while the implementation of Display() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable b and invoke Display(). The rite versions of Display get invoked based on the object the reference variable refers to. Time for a tiny teaser! Guess what the output would be in the following program?
Example 6
class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : DC
{

}

class Demo
{
public static void Main()
{
BC b;

b = new TC();
b.Display();
}
}
Output
DC::Display
Since TC has no implementation of Display(), it inherits Display() from DC as TC is derived from DC. Hence Display() from Derived class DC gets executed. It's as if the derived class TC looked like this:
class TC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}
to the compiler. Take one more example. Guess what its output will be.

Example 7

class BC
{
public virtual void Display()
{
System.Console.WriteLine("BC::Display");
}
}

class DC : BC
{
public override void Display()
{
System.Console.WriteLine("DC::Display");
}
}

class TC : DC
{
public new void Display()
{
System.Console.WriteLine("TC::Display");
}
}

class Demo
{
public static void Main()
{
BC b;

b = new TC();
b.Display();
}
}

Output

DC::Display
Agreed that TC defines its own new version of Display(). But its version of display is not invoked as Display() of TC does not override the Display() of the base class. With this understood we are done with Method overriding in C#.

Differences between Function overriding and Function overloading


Function Overloading Function Overriding
1) Both method names should be same,but signature should not be same.
1) Both method names,and signature and retun type should be same

2) In Overloading we will not use any virtual and override keywords 2) In overriding we have to use Virtual and Override keywords
3)Overloading we can implement with in single class or combination
of base and derived classes.

3)We can implement Overriding in base class and derived class but we can not implement in a single class