473,326 Members | 2,196 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,326 software developers and data experts.

Does C# 2.0 provide widecards of generics like Java?

I'm learning C# generics recenly. How do i do to write the similar Java
function below in C#?

void printCollection(Collection<? extends A> c) {
for (Object e: c) {
System.out.println(e);
}}

In printCollection, I have to limit the bound of the argument, when is a
generic of Collection with some classes inherites from A. Does anyone know
how I can do this in C#? Or if C# provides similar feature like widecards in
Java?
Nov 17 '05 #1
4 1618
"noname" <no****@discussions.microsoft.com> a écrit dans le message de news:
82**********************************@microsoft.com...
I'm learning C# generics recenly. How do i do to write the similar Java
function below in C#?

void printCollection(Collection<? extends A> c) {
for (Object e: c) {
System.out.println(e);
}}

In printCollection, I have to limit the bound of the argument, when is a
generic of Collection with some classes inherites from A. Does anyone know how I can do this in C#? Or if C# provides similar feature like widecards in Java?


You use the "where" syntax :

void printCollection<T>(Collection<T> c) where T : A
{
foreach (Object e in c)
Console.WriteLine(e);
}

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #2
noname wrote:
I'm learning C# generics recenly. How do i do to write the similar Java
function below in C#?

void printCollection(Collection<? extends A> c) {
for (Object e: c) {
System.out.println(e);
}}

In printCollection, I have to limit the bound of the argument, when is a
generic of Collection with some classes inherites from A. Does anyone
know
how I can do this in C#? Or if C# provides similar feature like widecards
in
Java?


There are no wildcards in C#. I'm not quite sure what the above code does,
exactly. Does it actually check, for the passed collection, that all
elements in the collection derive from A? In that case, there's no
equivalent in C#. Or does it filter the collection for elements that are
of type A? Also no equivalent. Or does it just let you assume that the
collection holds only A type elements? In that case, the foreach syntax
might do in C#:

public void PrintCollection(ArrayList list) {
// We don't really know what's going to be in list,
// but we assume all objects are deriving from A
foreach (A a in list)
// an exception will be thrown if an element shouldn't
// have the correct type
}

Usually, in C# you wouldn't do this kind of check in the parameter list of
a method. I can see how this mechanism might be handy, but it'll also be a
lot to do for the runtime (I'm assuming now that an actual check of some
kind is done on the collection passed in to the method). In C#, you'd use
a typed collection to begin with (List<A> comes to mind), so you can be
sure that it can only contain A type elements, no need to check that when
it's passed in somewhere. And for Generic classes you can also specify
constrainst for the types they are going to work with. Like this:

class MyClass<T> where T: A {
...
}

I'm not sure if this answers your question, though :-)
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #3
Joanna Carter (TeamB) wrote:
You use the "where" syntax :

void printCollection<T>(Collection<T> c) where T : A
{
foreach (Object e in c)
Console.WriteLine(e);
}


Ah, good hint :-) Yet another possibility I didn't consider because I
didn't know the exact meaning of the code in Java. I wasn't assuming that
this was a Generic method in itself.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #4
thanks a lot for your ansers.

my understanding is that the following code bounded the parameter of
printCollection. The parameter has to be a sub class of class A. Besides
"extends", you can also use "super" to set the lower bound of class
inheritance.
"Oliver Sturm" wrote:
noname wrote:
I'm learning C# generics recenly. How do i do to write the similar Java
function below in C#?

void printCollection(Collection<? extends A> c) {
for (Object e: c) {
System.out.println(e);
}}

In printCollection, I have to limit the bound of the argument, when is a
generic of Collection with some classes inherites from A. Does anyone
know
how I can do this in C#? Or if C# provides similar feature like widecards
in
Java?


There are no wildcards in C#. I'm not quite sure what the above code does,
exactly. Does it actually check, for the passed collection, that all
elements in the collection derive from A? In that case, there's no
equivalent in C#. Or does it filter the collection for elements that are
of type A? Also no equivalent. Or does it just let you assume that the
collection holds only A type elements? In that case, the foreach syntax
might do in C#:

public void PrintCollection(ArrayList list) {
// We don't really know what's going to be in list,
// but we assume all objects are deriving from A
foreach (A a in list)
// an exception will be thrown if an element shouldn't
// have the correct type
}

Usually, in C# you wouldn't do this kind of check in the parameter list of
a method. I can see how this mechanism might be handy, but it'll also be a
lot to do for the runtime (I'm assuming now that an actual check of some
kind is done on the collection passed in to the method). In C#, you'd use
a typed collection to begin with (List<A> comes to mind), so you can be
sure that it can only contain A type elements, no need to check that when
it's passed in somewhere. And for Generic classes you can also specify
constrainst for the types they are going to work with. Like this:

class MyClass<T> where T: A {
...
}

I'm not sure if this answers your question, though :-)
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #5

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

Similar topics

0
by: mark | last post by:
I've been getting odd compile errors when trying to sort while using generics. I have the following code: static final List<Class> levelList; static { Vector<Class> v = new Vector<Class>();...
24
by: Gary van der Merwe | last post by:
Hi When C# 2.0 arrives System.Collections.Specialized.StringCollection will become "obsolete". Will the framework still contain this class, or will there be a C# 1.X to 2.0 conversion utility...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
2
by: Locia | last post by:
What are the principal differences in term of type safe, performance, implementation?
6
by: Peter Olcott | last post by:
Does the 2005 release of Visual Studio provide either STL (the C++ Standard Template Library) or an equivalent for C#? I am most interested in the 2005 C# equivalent of std::vector.
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
16
by: coosa | last post by:
Dear all, I'm familiar with the Set Class in C++ and Java; however, i have been searching in C# for a similar container as in c++ or collection as in java, but couldn't find one. Could some one...
21
by: Peter Duniho | last post by:
On Fri, 18 Jul 2008 07:03:37 -0700, Ben Voigt <rbv@nospam.nospamwrote: I agree whole-heartedly about being closer to Java. But the OP didn't ask about Java. :) I disagree on the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.