Genrics questions

C

Chun Wang

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?
 
J

Jay B. Harlow [MVP - Outlook]

Chun,
In case you don't have them the following article provides some good info on
Generics
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/default.aspx
http://msdn.microsoft.com/msdnmag/issues/03/10/NET/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/team/language/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/lhsdk/ref/system.collections.generic.aspx
http://longhorn.msdn.microsoft.com/lhsdk/ref/ns/system/s/optionalvalue/optionalvalue.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
 
M

Mattias Sjögren

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.

Not sure. I want to say yes.
No


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

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.

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/productinfo/roadmap.aspx



Mattias
 
J

Jay B. Harlow [MVP - Outlook]

Mattias,
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.
I was confusing specialization with deriving from a generic type:

public class Calculator: Calculator said:
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 ;-)
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.
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
 
T

Taylor

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
 
J

Jon Skeet [C# MVP]

Taylor said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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
 
M

Mattias Sjögren

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
 
E

Eric Gunnerson [MS]

--
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 said:
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.
 

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

Top