Can generic types be overloaded?

  • Thread starter Thread starter muler
  • Start date Start date
M

muler

Hi,

[Section 20.1 The C# Programming Language;Anders Hejlsberg, Scott
Wiltamuth, Peter Golde]
"
Generic types may not be "overloaded"; that is, the identifier of a
generic type must be uniquely named within a scope in the same way as
ordinary types.

class C {}
class C<V> {} // Error, C defined twice
class C<U,V> {} // Error, C defined twice

"
But I was unable to verify this using Microsoft Visual C# 2005, which
reports no error:

class Program
{

class C { }

class C<V> { } // no error reported

class C<U, V> { } // no error reported

static void Main(string[] args)
{

}
}

Is this an error in the book or something else?

Thanks!
Mulugeta.
 
muler said:
Hi,

[Section 20.1 The C# Programming Language;Anders Hejlsberg, Scott
Wiltamuth, Peter Golde]
"
Generic types may not be "overloaded"; that is, the identifier of a
generic type must be uniquely named within a scope in the same way as
ordinary types.

class C {}
class C<V> {} // Error, C defined twice
class C<U,V> {} // Error, C defined twice

"
But I was unable to verify this using Microsoft Visual C# 2005, which
reports no error:

class Program
{

class C { }

class C<V> { } // no error reported

class C<U, V> { } // no error reported

static void Main(string[] args)
{

}
}

Is this an error in the book or something else?

Looks like an error in the book. While C++ templates support default
arguments, generics do not, so differing numbers of generic parameters can
give you the same result. This is allowed because the number of generic
arguments is part of the mangled name (List`1, Dictionary`2 for instance).

However, you still cannot overload generics. For example, I believe the
following will not compile:

class C<U, V> where U : List<V> {}
 
muler said:
[Section 20.1 The C# Programming Language;Anders Hejlsberg, Scott
Wiltamuth, Peter Golde]
"
Generic types may not be "overloaded"; that is, the identifier of a
generic type must be uniquely named within a scope in the same way as
ordinary types.

class C {}
class C<V> {} // Error, C defined twice
class C<U,V> {} // Error, C defined twice

"

I believe that book came out before VS2005 (in fact I'm sure it did - I
got a copy in September 2005), and I believe the language spec changed
a little bit after that. Here's the relevant section from the ECMA spec
(freely downloadable):

<quote>
Generic types can be =3Foverloaded=3F on the number of type parameters;
that is two type declarations within the same namespace or outer type
declaration can use the same identifier as long as they have a
different number of type parameters.
class C {}
class C<V> {} // OK
struct C<U,V> {} // OK
class C<A,B> {} // Error, C with two type parameters defined twice
</quote>
 
muler said:
[Section 20.1 The C# Programming Language;Anders Hejlsberg, Scott
Wiltamuth, Peter Golde]
"
Generic types may not be "overloaded"; that is, the identifier of a
generic type must be uniquely named within a scope in the same way as
ordinary types.
class C {}
class C<V> {} // Error, C defined twice
class C<U,V> {} // Error, C defined twice

I believe that book came out before VS2005 (in fact I'm sure it did - I
got a copy in September 2005), and I believe the language spec changed
a little bit after that. Here's the relevant section from the ECMA spec
(freely downloadable):

<quote>
Generic types can be =3Foverloaded=3F on the number of type parameters;
that is two type declarations within the same namespace or outer type
declaration can use the same identifier as long as they have a
different number of type parameters.
class C {}
class C<V> {} // OK
struct C<U,V> {} // OK
class C<A,B> {} // Error, C with two type parameters defined twice
</quote>

Thanks!
 
Back
Top