Dheeman Dutta

Just Another Blog.....

  Home  |   Contact  |   Syndication    |   Login
  39 Posts | 1 Stories | 29 Comments | 12 Trackbacks

News



Archives

Post Categories

.NET Links

Just to add to my previous blog abt Generics Overloading the folowing scenarion is also very helpful.....

Suppose I have a class

public class TestClass<T, U>

{

public void Foo<I>(T val1, I val2, U val3) { }

public void Foo<I>(U val1, T val2, I val3) { }

}

And I'm calling this as

static void Main(string[] args)

{

TestClass<int, string> ob = new TestClass<int, string>();

ob.Foo<double>(1, 122223, "er");

}

it will compile pefectly as the permutations of U,T,and I are always unique....However if the call would be

static void Main(string[] args)

{

TestClass<int, int> ob = new TestClass<int, int>();

ob.Foo<double>(1, 122223, 2);

}

 

it would give a compile error.

posted on Tuesday, September 12, 2006 9:29 AM

Feedback

# re: More abt Generics Overloading 9/12/2006 7:06 PM Nuri Halperin
The compiler can generate an instance
TestClass<int,string>() because it's overloads would be
Foo<I>(int, I, string)
and
Foo<I>(string, int, I)
which easily meet the disambiguating rules for method signatures.

But when you try to do a TestClass<int,int>
the compiler starts building the Foo's:
Foo<I>(int, I, int) // ok.. firs one is fine
then then next foo,
Foo<I>(int, int, I) // oops!

At this point, the compiler can't guarantee parameter type disambiguity. Tha't because I is not instantiated and can't be verified.

Good compiler behavior! It *should* stop you from doing this. Method overload rules state that functions should differ by parameter types and number. Your overloads don't.

Moving 'I' around in the parameter order does little here because I is not in Test<T,U> class declaration.

# re: More abt Generics Overloading 9/13/2006 6:23 AM Dheeman
This is exactly what i meant
thanks mate

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: