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

Home Posts Topics Members FAQ

Generic ICollection and interfaces

I have a public sealed Class A that implements the interface I.
Now I want to pass an ICollection<Aas a parameter to a method that takes a
parameter of the type ICollection<Ibu t I get the error:

Can't convert from System.Collecti ons.Generic<Ato
System.Collecti ons.Generic<I>

Shouldn't I be able to pass ICollection<Ato a method that takes
ICollection<Iwh en A implements I ?

Do I really need to cast ICollection<Ato ICollection<Ibe fore passing it?

Kind Regards,
Allan Ebdrup
Oct 31 '06 #1
2 1965
Casting won't work...

What you nead is a generic method:

public void SomeMethod<T>(I Collection<Tite ms)
where T : ISomeInterface
{

}
You should find that you can then call this without specifying the generic
parameter:

ICollection<Amy Items = //TODO; needs A : ISomeInterface
SomeMethod(myIt ems);

Marc
Oct 31 '06 #2

"Marc Gravell" <ma**********@g mail.comwrote in message
news:u5******** ******@TK2MSFTN GP02.phx.gbl...
Casting won't work...

What you nead is a generic method:

public void SomeMethod<T>(I Collection<Tite ms)
where T : ISomeInterface
{

}
You should find that you can then call this without specifying the generic
parameter:

ICollection<Amy Items = //TODO; needs A : ISomeInterface
SomeMethod(myIt ems);
Thanks that did the trick.

Kind Regards,
Allan Ebdrup
Oct 31 '06 #3

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

Similar topics

2
1653
by: PMarino | last post by:
Hi all - I'm sure I'm being brain-dead here - and I think the answer is 'you can't do that', but I thought I would try. I have an interface that is Generic, with two type arguments. I want to create a second interface that specifices the second type argument. I also want to have objects implement that second interface without having to implement the more generic methods that are inherited from the first. This is especially evident...
4
2226
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because the old code allowed me to do what basically was the following assignment: class SomeClass { private Queue q; SomeClass(Queue q)
2
2229
by: anders.forsgren | last post by:
I have a generic collection looking like this Set<T> : ICollection<T> { // Create set from existing collection. public Set(ICollection<T> objs) {/* */} // Add objects to set public void AddRange(ICollection<T> objs){ /* */ } }
5
1987
by: Brian Richards | last post by:
Without generics generally I'd dervie from CollectionBase to create typed collections. Generics mostly fill in this gap except that there are no virtual methods on List<T> so we can no longer notify other object when a collection changes during add, remove and set operations. I was curious how Others were getting around this problem? I thought of doing something like: class MyCollection : IList<T>, ICollection<T> { private List<T>...
2
1493
by: Angel Mateos | last post by:
I have this structure: Class ElemBase Class Elem1 : Inherits ElemBase Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits System.ComponentModel.BindingList(Of GenElem) Class Colec1 : Inherits ColecBase(Of Elem1)
2
2816
by: Edward Diener | last post by:
Following the example in the help for the generic List class, which shows the Serializable attribute being used on the generic class, like so: generic<typename T> public ref class List : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable I try to do the same for my own generic class, like so:
5
4192
by: Michi Henning | last post by:
I can pass a generic collection as ICollection<Tjust fine: static void flatCollection(ICollection<intc) {} // ... List<intl = new List<int>(); flatCollection(l); // Works fine Now I want to pass nested collections generically:
2
2647
by: confused1234 | last post by:
Hi i'm trying to pass a field of type System.Collections.Generic.ICollection< FeedImage> to a function. I dont't have a clue what this icollection is. but i tried some code System.Collections.Generic.ICollection< FeedImage> imagea ; FeedImage fee = new FeedImage ("http://www.image.com/image.jpg", "http://www.image.com");
15
2396
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a bit akin to ask: "the System.Web.UI and System.Windows.Controls namespace both contains a Control class, could I use one in place of the other? common they have the same name!" If you want to use a method common to both you should do as Alun...
0
9793
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
10774
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
10489
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...
0
10202
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9314
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...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4411
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
2
3959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.