any possible use of this feature?

  • Thread starter Thread starter Justin Shen
  • Start date Start date
J

Justin Shen

you can define two class with same name but having different generic
parameters in one assembly. As below:

class Gen<T>
{
}

class Gen<T1,T2>
{
}

any possible use of it?
 
class name has to be unique within one namespace. If you have to define two
classes with same name, you can put them in different namespaces in one
assembly. C# class does not support template taking parameters like in C++
 
Jiachuan Wang said:
class name has to be unique within one namespace. If you have to define two
classes with same name, you can put them in different namespaces in one
assembly. C# class does not support template taking parameters like in C++

I think Justin was actually talking about the generics which will be
available in C# v2.
 
Justin Shen said:
you can define two class with same name but having different generic
parameters in one assembly. As below:

class Gen<T>
{
}

class Gen<T1,T2>
{
}

any possible use of it?

Possibly - I could imagine something which would be like an array-list
but with multiple "dimensions", each of which would be a different
type. For instance, you could do:

myList[10,"hello"] = "there";

With straight generics on ArrayList you could easily get to

myList[10]["hello"] = "there";

I suspect, but the first syntax might be better in some circumstances.

Probably not *really* useful, but just an idea of how it might be used.
 
Justin Shen said:
you can define two class with same name but having different generic
parameters in one assembly. As below:

class Gen<T>
{
}

class Gen<T1,T2>
{
}

any possible use of it?
I don't think you'll find *many* useful uses for it. In most of the
scenarios I can think of, a base class would have to exist already, often
times making the usage of generics less valuable. In those cases I can think
of a particular usage of this that wouldn't require a new base class(where
Gen<T> could be used as the base), I think the other class should probably
have an entirely different name.

It does make one wonder if syntax could be formed that would allow a sort of
generic overloading, so that one class could take, for example, from 1 to 3
types. I'm not sure of the explicit value of that either, but it would be
more interesting and probably more flexible than the above concept.
 
Justin said:
you can define two class with same name but having different generic
parameters in one assembly. As below:

class Gen<T>
{
}

class Gen<T1,T2>
{
}

Assuming you're talking about C# 2.0 (Whidbey), are you sure that you
can do this? I haven't played around with Whidbey much, but my reading
of the draft spec indicates that this is not supported (from Section 20.1):

=================================================================
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

However, the type lookup rules used during unqualified type name lookup
(§20.9.3) and member access (§20.9.4) do take the number of generic
parameters into account.
=================================================================

The exceptions listed above allow generic types in, for example,
different namespaces to have the same 'simple name' with differing type
parameters. But, since they are in different namespaces they are really
completely unrelated (other than they they happen to share the same
simple name).

I think this is one of the reasons that you find the generic classes in
..NET 2.0 in separate namespaces from their non-generic counterparts.

On the other hand, I don't see the reason why they couldn't allow type
parameters to overload class names, but I'm not a language design
expert, so there's probably a very good reason.
 
Justin,
One place I would consider using it, is if I wanted a "performant" Key class
as identified under the Identity Field pattern in Martin Fowler's book
"Patterns of Enterprise Application Architecture".
http://www.martinfowler.com/eaaCatalog/identityField.html (you need the book
to see the sample of the Key class).

By "performant" I mean one that is not based on an array of object, as
Martin shows in his book. One that would be based on the actual types of
each key field, plus require the actual number of key fields expected.

Of course there is the expense of defining each key type...

class Key

class Key<T> : Key, IComparable, IComparable<Key<T>> where T :
IComparable<T>

class Key<T1, T2> : Key, IComparable, IComparable<Key<T1, T2>> where T1
: IComparable<T1> where T2 : IComparable<T2>
...

class Key<T1, T2, T3>
...

I would consider defining a base Key type as above that would allow the Key
to be polymorphic with the Layer Super Type.

IComparable<T> is rather cool, one thing I have not figured out is how to
get a non-boxing GetHashCode from a generic...

Just a thought
Jay

Of course I would have constraints on the types, so they implemented
IComparable<Key<T>>
 
Additional:
IComparable<T> is rather cool, one thing I have not figured out is how to
get a non-boxing GetHashCode from a generic...
C# allows me to call GetHashCode on a generic field, however VB.NET does not
allow me to call GetHashCode, seeing as GetHashCode is (ultimately) Object,
and all generic fields ultimately inherit from Object, I would expect to be
able to call it, I will pursue this in the private Whidbey newsgroups.

Thanks
Jay
 
yeah, i am talking about c# 2.0.

and i had tried the code in Whidbey CTP release. It compiles and runs well
so i got the question above. :)
 
i had tried the code i listed above in Whidbey CTP release, and the two
class are within the same namespace and even within the same assembly. The
code compiles and i was able to declare two instance of the two class. Hope
it is not a bug and will be removed in the final release. :-p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top