473,603 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.prin tln(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 1632
"noname" <no****@discuss ions.microsoft. com> a écrit dans le message de news:
82************* *************** **...icrosof t.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.prin tln(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.WriteLi ne(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.prin tln(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.WriteLi ne(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.prin tln(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
5560
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>(); v.add(myClass1.class);
24
1696
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 that will change System.Collections.Specialized.StringCollection to System.Collections.ArrayList <string> and ArrayList to ArrayList<object> ?? Gary
27
2452
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 still no can do... Any info would be great!
17
2668
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 cboFont_DrawItem event handler? Why does it need to be cast? -hazz ,................. cboFont.Items.AddRange(FontFamily.Families); } private void cboFont_DrawItem(object sender,
2
1406
by: Locia | last post by:
What are the principal differences in term of type safe, performance, implementation?
6
2501
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
21043
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 simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
16
2110
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 tell me about the the Set implementation inside .NET? Best regards
21
2254
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 VB/VB.NET point, but I guess that's a matter of opinion so not entirely unexpected. In spite of some fundamental conceptual differences between C++ and C#, I find moving back and forth between those
0
7996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8415
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8405
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5878
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2430
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 we have to send another system
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.