473,497 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Genrics questions


1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?

For example:

pulic class MyStack<T, int>
{
.....
}

Here maybe I want to establish a upper limit of the stack.

2. Does the implemention support specialization? (I mean
something like C++ template specialization) This is an
extremely useful language feature in C++ template.

for example:

public class MyStack<T>
{
....
}

public class MyStack<string>
{
....
}

This is not a good sample, but it explains what I paray
for.

3. For operator as and is, does it support genric classes?

For example:

class MyNode<T>
{
....
}
class MyList<T>
{
....
public void MyMethod(T t)
{
if(t is MyNode<T>)
{
MyNode<T> obj = t as MyNode<T>;
}
....
}
Again this is a bad sample. But the question is, am I
allowed to write code like this?

4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level
when developing subcalsses. The compiler sure can infer
all these constraints at baseclass level. Is there any
technical difficulty or sound reason for compiler not to
do it?

5. It's thrilling to learn that Microsoft will provide
a .NET version of STL. But can't find more information
about it. What is expected to this .NET version STL?
What's missing and What's improved?

6. Can I overload operators for generic classes? If I can,
can I have extra genric type parameter for such operators?
Nov 15 '05 #1
8 2664
Chun,
In case you don't have them the following article provides some good info on
Generics
http://msdn.microsoft.com/msdnmag/is...T/default.aspx
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

Also you can download the C# version 2.0 Language Specification which
includes "complete" details on Generics in C#.

http://msdn.microsoft.com/vcsharp/te...e/default.aspx
1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?
pulic class MyStack<T, int> I believe you need to use a constraint, you can use constraints for base
types & interfaces & default constructors, I have not tried using it for a
value type. I would expect it to work as a base type.

public class MyStack<T, X > where X : int
{
}
2. Does the implemention support specialization? Not sure. I want to say yes.
3. For operator as and is, does it support genric classes? I understand that they do.
4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level Not sure, I thought it was odd I had to repeat them also.
5. It's thrilling to learn that Microsoft will provide
a .NET version of STL. Really, where did you here this? I only know of the
System.Collections.Generic namespace. Plus the System.OptionalValue
structure. (someplaces refer to OptionalValue as Nullable). Which is not
quite the STL by any means. ;-)

http://longhorn.msdn.microsoft.com/l...s.generic.aspx
http://longhorn.msdn.microsoft.com/l...onalvalue.aspx
6. Can I overload operators for generic classes? Yes
If I can,
can I have extra genric type parameter for such operators? Not sure what you mean. Generic Overloaded Operators still follow the normal
rules for Overloading Operators & for generic methods. I want to say the C#
2.0 specification had details of what is & is not allowed on overloaded
operators...

Hope this helps
Jay

"Chun Wang" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?

For example:

pulic class MyStack<T, int>
{
....
}

Here maybe I want to establish a upper limit of the stack.

2. Does the implemention support specialization? (I mean
something like C++ template specialization) This is an
extremely useful language feature in C++ template.

for example:

public class MyStack<T>
{
...
}

public class MyStack<string>
{
...
}

This is not a good sample, but it explains what I paray
for.

3. For operator as and is, does it support genric classes?

For example:

class MyNode<T>
{
...
}
class MyList<T>
{
...
public void MyMethod(T t)
{
if(t is MyNode<T>)
{
MyNode<T> obj = t as MyNode<T>;
}
...
}
Again this is a bad sample. But the question is, am I
allowed to write code like this?

4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level
when developing subcalsses. The compiler sure can infer
all these constraints at baseclass level. Is there any
technical difficulty or sound reason for compiler not to
do it?

5. It's thrilling to learn that Microsoft will provide
a .NET version of STL. But can't find more information
about it. What is expected to this .NET version STL?
What's missing and What's improved?

6. Can I overload operators for generic classes? If I can,
can I have extra genric type parameter for such operators?

Nov 15 '05 #2
1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?
pulic class MyStack<T, int>
I don't see when or how that would be useful. Can you (Chun Wang)
provide additional use cases. To provide an upper limit on a stack
class, why not just pass it as a regular parameter to the constructor?

I believe you need to use a constraint, you can use constraints for base
types & interfaces & default constructors, I have not tried using it for a
value type. I would expect it to work as a base type.

public class MyStack<T, X > where X : int
{
}


You can't use value types as type constraints. It would be sort of
useless, since valuetypes can't be derived from, so the only valid
type parameter would in that case be int itself.

2. Does the implemention support specialization?

Not sure. I want to say yes.


No

3. For operator as and is, does it support genric classes?

I understand that they do.


They sure are supported in the way the OP wanted it.

But given this

class Base {}
class Derived : Base {}
List<Derived> ld = new List<Derived>();

this will evaluate to false, which might surprise people

if ( ld is List<Base> )

4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level

Not sure, I thought it was odd I had to repeat them also.


Personally I much prefer if they have to be explicitly given in every
subclass, so I don't have to locate all base classes to see the
complete set of constraints.

5. It's thrilling to learn that Microsoft will provide
a .NET version of STL.

Really, where did you here this?


I think he's referring to this, which is a C++ thing, not part of the
BCL.

"First, a new version of the Standard Template Library (STL) will be
introduced. This version of STL will be tuned for interacting with
managed code and data. Programmers who are comfortable using STL for
writing traditional C++ applications will find they will be able to
apply the same coding techniques to writing CLR-based applications."

http://msdn.microsoft.com/vstudio/pr...o/roadmap.aspx

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #3
Mattias,
1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?
pulic class MyStack<T, int>
I don't see when or how that would be useful. Can you (Chun Wang)
provide additional use cases. To provide an upper limit on a stack
class, why not just pass it as a regular parameter to the constructor? Double checking C++ templates, what I suggested is not even close what he
was asking. :-(

In C++ Chun Wang's example is:

pulic class MyStack<typename T, int C>
{
T list[C];
}

Which allows you to define how large the array is when you create the new
type:

MyStack<int, 50> a;
MyStack<int, 40> b;
MyStack<string, 10> c;
MyStack<string, 15> d;

would define 4 variables of 4 distinct types.
You can't use value types as type constraints. It would be sort of
useless, since valuetypes can't be derived from, so the only valid
type parameter would in that case be int itself. Are you sure about value types? What about sealed classes? They are still
reference types, however you cannot derive from them... I admit/agree
constraining a type parameter to a single class doesn't really make any
sense, however I don't know why the compiler would actually bother to check
for that condition.
2. Does the implementation support specialization?

Not sure. I want to say yes.


No

I was confusing specialization with deriving from a generic type:

public class Calculator: Calculator<int>
3. For operator as and is, does it support genric classes? They sure are supported in the way the OP wanted it.
this will evaluate to false, which might surprise people
if ( ld is List<Base> )

I understand why it returns false ;-)
4. constraint is an elegant solution for some long-time

Not sure, I thought it was odd I had to repeat them also.

Personally I much prefer if they have to be explicitly given in every
subclass, so I don't have to locate all base classes to see the
complete set of constraints.

I thought it was odd when I first read it, overall I think I will agree with
your statement, once I play with them more.
5. It's thrilling to learn that Microsoft will provide
a .NET version of STL.

Really, where did you here this?

I think he's referring to this, which is a C++ thing, not part of the
BCL.
"First, a new version of the Standard Template Library (STL) will be

I follow you. Now my question is: Is that a C++ "only" thing or is it
available to any .NET language?

Yes its for Managed C++, but it doesn't really say whether its for C++ only
or any Managed Language.

Thanks for the additional info.

Jay

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?
pulic class MyStack<T, int>
I don't see when or how that would be useful. Can you (Chun Wang)
provide additional use cases. To provide an upper limit on a stack
class, why not just pass it as a regular parameter to the constructor?

I believe you need to use a constraint, you can use constraints for base
types & interfaces & default constructors, I have not tried using it for avalue type. I would expect it to work as a base type.

public class MyStack<T, X > where X : int
{
}


You can't use value types as type constraints. It would be sort of
useless, since valuetypes can't be derived from, so the only valid
type parameter would in that case be int itself.

2. Does the implemention support specialization?

Not sure. I want to say yes.


No

3. For operator as and is, does it support genric classes?

I understand that they do.


They sure are supported in the way the OP wanted it.

But given this

class Base {}
class Derived : Base {}
List<Derived> ld = new List<Derived>();

this will evaluate to false, which might surprise people

if ( ld is List<Base> )

4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level

Not sure, I thought it was odd I had to repeat them also.


Personally I much prefer if they have to be explicitly given in every
subclass, so I don't have to locate all base classes to see the
complete set of constraints.

5. It's thrilling to learn that Microsoft will provide
a .NET version of STL.

Really, where did you here this?


I think he's referring to this, which is a C++ thing, not part of the
BCL.

"First, a new version of the Standard Template Library (STL) will be
introduced. This version of STL will be tuned for interacting with
managed code and data. Programmers who are comfortable using STL for
writing traditional C++ applications will find they will be able to
apply the same coding techniques to writing CLR-based applications."

http://msdn.microsoft.com/vstudio/pr...o/roadmap.aspx

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #4
Does Visual Studio .NET support Generics in C#? I know Whidbey will, but
does the current release of VS support it too?

Sorry for being a bit off the topic from Chun's...Taylor
"Chun Wang" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...

1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?

For example:

pulic class MyStack<T, int>
{
....
}

Here maybe I want to establish a upper limit of the stack.

2. Does the implemention support specialization? (I mean
something like C++ template specialization) This is an
extremely useful language feature in C++ template.

for example:

public class MyStack<T>
{
...
}

public class MyStack<string>
{
...
}

This is not a good sample, but it explains what I paray
for.

3. For operator as and is, does it support genric classes?

For example:

class MyNode<T>
{
...
}
class MyList<T>
{
...
public void MyMethod(T t)
{
if(t is MyNode<T>)
{
MyNode<T> obj = t as MyNode<T>;
}
...
}
Again this is a bad sample. But the question is, am I
allowed to write code like this?

4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level
when developing subcalsses. The compiler sure can infer
all these constraints at baseclass level. Is there any
technical difficulty or sound reason for compiler not to
do it?

5. It's thrilling to learn that Microsoft will provide
a .NET version of STL. But can't find more information
about it. What is expected to this .NET version STL?
What's missing and What's improved?

6. Can I overload operators for generic classes? If I can,
can I have extra genric type parameter for such operators?

Nov 15 '05 #5
Taylor <ta****@u.washington.edu> wrote:
Does Visual Studio .NET support Generics in C#? I know Whidbey will, but
does the current release of VS support it too?


No - because the current release of .NET doesn't support it either.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Chun
Additional info:
2. Does the implemention support specialization? (I mean
something like C++ template specialization) This is an
extremely useful language feature in C++ template. I believe "specialization" should be (is?) supported for generic methods,
however I believe Mattias is correct in that for generic types it is not
supported.

For generic methods you are (should be) able to overload the method. C# &
VB.NET will use inference to find the method, seeing as there is a specific
overload for the method that should be called, otherwise a generic version
should be created. I not seeing specifically where this is allowed or not
allowed in the C# 2.0 specification.

I have not tried it in C# yet, but I've been playing with it in VB.NET. I am
able to overload a Generic method, with a specific version of the method.
Unfortunately I occasionally get an error when I attempt to call the method.

Given the overloaded VB.NET Whidbey functions:

Public Function IIf(Of T) (exp As Boolean, tp as T, fp As T) As T
If exp Then
Return tp
Else
Return fp
End If
End Function

Public Function IIf(exp As Boolean, tp As Single, fp As Single) As
Single
If exp Then
Return tp
Else
Return fp
End If
End Function

If I use:
Dim f As Single
f = IIF(True, 1, 2.0F)

VB.NET will call the specialized version of the function, however if I use:

f = IIf(True, 1.0F, 2.0F)

I get a compile error about "overload resolution failed because no
accessible IIF is most specific for these arguments".

I would expect the specialized "IIF(boolean, single, single)" to be called
in both cases.

Of course if I call
Dim s As String
s = IIf(True, "Empty", "Full")

The IIf(Of String) version will be called.

Hope this helps
Jay

"Chun Wang" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?

For example:

pulic class MyStack<T, int>
{
....
} Here maybe I want to establish a upper limit of the stack.

2. Does the implemention support specialization? (I mean
something like C++ template specialization) This is an
extremely useful language feature in C++ template.

for example:

public class MyStack<T>
{
...
}

public class MyStack<string>
{
...
}

This is not a good sample, but it explains what I paray
for.

3. For operator as and is, does it support genric classes?

For example:

class MyNode<T>
{
...
}
class MyList<T>
{
...
public void MyMethod(T t)
{
if(t is MyNode<T>)
{
MyNode<T> obj = t as MyNode<T>;
}
...
}
Again this is a bad sample. But the question is, am I
allowed to write code like this?

4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level
when developing subcalsses. The compiler sure can infer
all these constraints at baseclass level. Is there any
technical difficulty or sound reason for compiler not to
do it?

5. It's thrilling to learn that Microsoft will provide
a .NET version of STL. But can't find more information
about it. What is expected to this .NET version STL?
What's missing and What's improved?

6. Can I overload operators for generic classes? If I can,
can I have extra genric type parameter for such operators?

Nov 15 '05 #7
Jay,
In C++ Chun Wang's example is:

pulic class MyStack<typename T, int C>
{
T list[C];
}

Which allows you to define how large the array is when you create the new
type:

MyStack<int, 50> a;
MyStack<int, 40> b;
MyStack<string, 10> c;
MyStack<string, 15> d;

would define 4 variables of 4 distinct types.
Right, but I don't see why he wants the size to be associated with the
type, and not the instance.

Are you sure about value types?
Yes

What about sealed classes?
You can't use those either.

System.{Array, Enum, Delegate, ValueType} are also disallowed, at
least by the C# compiler.

They are still
reference types, however you cannot derive from them... I admit/agree
constraining a type parameter to a single class doesn't really make any
sense, however I don't know why the compiler would actually bother to check
for that condition.
Why not? :-) The more compile time checks the better.

I follow you. Now my question is: Is that a C++ "only" thing or is it
available to any .NET language?


I believe it's a C++ only thing, built with C++ templates, not
generics. I think the purpose is to bridge STL and .NET BCL concepts
such as iterators and IEnumerator and so on.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #8


--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Chun Wang" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...

1. When writing genric classes or methods, can I use
specifc type (instead of generic type) as type parameter?

For example:

pulic class MyStack<T, int>
{
....
}

Here maybe I want to establish a upper limit of the stack.
No. In C#, you would do this by passing a parameter to the constructor to do
that.

2. Does the implemention support specialization? (I mean
something like C++ template specialization) This is an
extremely useful language feature in C++ template.

for example:

public class MyStack<T>
{
...
}

public class MyStack<string>
{
...
}

This is not a good sample, but it explains what I paray
for.
..NET generics do not support specialization in the Whidbey release. They may
in future releases

3. For operator as and is, does it support genric classes?

For example:

class MyNode<T>
{
...
}
class MyList<T>
{
...
public void MyMethod(T t)
{
if(t is MyNode<T>)
{
MyNode<T> obj = t as MyNode<T>;
}
...
}
Again this is a bad sample. But the question is, am I
allowed to write code like this?
You should be able to write code like this.


4. constraint is an elegant solution for some long-time
issues for C++ template. But I think it will be very
tedious to repeat every constraints at basecalss level
when developing subcalsses. The compiler sure can infer
all these constraints at baseclass level. Is there any
technical difficulty or sound reason for compiler not to
do it?
I'm not sure I understand the question. If you're deriving a generic class
from a generic base class, the constraints in the base class would still
apply.

5. It's thrilling to learn that Microsoft will provide
a .NET version of STL. But can't find more information
about it. What is expected to this .NET version STL?
What's missing and What's improved?

6. Can I overload operators for generic classes? If I can,
can I have extra genric type parameter for such operators?
No.

Nov 15 '05 #9

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

Similar topics

0
4068
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
4544
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
7150
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
2493
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
7948
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
1472
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
1600
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
4465
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
3410
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
2916
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
7120
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
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.