473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

any possible use of this feature?

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?
Nov 16 '05 #1
9 1228
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++

--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Justin Shen" <De*******@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
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?

Nov 16 '05 #2
Jiachuan Wang [MSFT] <ji*****@online.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Justin Shen <De*******@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

"Justin Shen" <De*******@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 16 '05 #5
Justin Shen wrote:
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.

--
mikeb
Nov 16 '05 #6
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/eaaCatal...tityField.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>>
"Justin Shen" <De*******@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
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?

Nov 16 '05 #7
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O7**************@TK2MSFTNGP11.phx.gbl... 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/eaaCatal...tityField.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>>
"Justin Shen" <De*******@hotmail.com> wrote in message
news:es**************@TK2MSFTNGP10.phx.gbl...
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?


Nov 16 '05 #8
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. :)
"Jon Skeet [C# MVP]" <sk***@pobox.com> ????
news:MP************************@msnews.microsoft.c om...
Jiachuan Wang [MSFT] <ji*****@online.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9
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

"mikeb" <ma************@nospam.mailnull.com> ????
news:OU*************@TK2MSFTNGP11.phx.gbl...
Justin Shen wrote:
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.

--
mikeb

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
8
by: Nick | last post by:
Im trying to insert a bunch of rows into a table. If the row already exists id like to update the row 'counter'. For example... INSERT INTO table1 SELECT field1, field2 FROM table2 ON...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
2
by: shlomi.schwartz | last post by:
Hi all, I'm trying to open a new non rectangular dialog window from an HTA application, is it possible? I tried using window.showModalDialog() with the unadorned feature as a starting point...
3
by: Jiho Han | last post by:
If there is a dll that my asp pages uses but is in a separate assembly, can I define a separate config for it and will it read? For example, if I have a MyLibrary.dll and I define a...
11
by: ano | last post by:
Hi, I'm a C# newbie. My application (Solution) contains two Windows application projects. They create their own ".exe" output that means there are 2 Main() methods in one solution. Is is...
9
by: Andy | last post by:
Hi there, I'm trying to do some predicting work over user input, here's my question: for pattern r'match me', the string 'no' will definitely fail to match, but 'ma' still has a chance if...
2
by: | last post by:
I'd like to make a tool that non-technicians could invoke if there was a defacement or destruction of our database data for whatever reason and an expert wasn't around to tend to things. The...
9
by: Cristian | last post by:
algebraic expression 'a*b+c' with CIN .Is it possible? How to transfer the algebraic expression 'a*b+c' to the variable s (all double) with cout in a "Console Application" ? cout<<"Input a,b,c...
1
by: robin1983 | last post by:
Dear All, How are you everyone! I need a help from you all. Firstly, I want to know is it possible to fetch the email details from the Outlook to Excel or Database by using PHP. MS outlook has...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.