Monday, August 9, 2010

Method Overloading

1) Class can not be declared as Private.

Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

namespace OOPS
{
private class A
{
}
}

By default it is Public

2) Method must have a return type
class A
{
public M()
{
}
}

3) Type 'OOPS.A' already defines a member called 'M' with the same parameter types

class A
{
public void M()
{
}
public void M()
{
}
}

4) True Method Overload
class A
{
public void M(int i)
{
}
public void M()
{
}
}

5) Type 'OOPS.A' already defines a member called 'M' with the same parameter types

class A
{
public void M()
{
}
public string M()
{
return "HI";
}
}

6) Method Overload

class A
{
public void M()
{
}
public string M(int i)
{
return "HI";
}
}





7) Method Overload
public void M(int i)
{
}
private string M()
{
return "HI";
}

8) Method Overload
class A
{
public void M(Int16 i)
{
}
public void M(Int32 i)
{
}
public void M(Int64 i)
{
}
}

9) Type 'OOPS.A' already defines a member called 'M' with the same parameter types

public void M(int i)
{
}
public void M(Int32 i)
{
}
10)Method Overload
class A
{
public void M(int i)
{
}
public void M(double i)
{
}
}

Conclusion: Access modifier and return type of function does not matter in Method Overloading. What matter is
1) Number of function parameters.
2) Datatype of parameters

If same parameters then overloading is not possible.

No comments:

Post a Comment