473,383 Members | 1,892 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,383 software developers and data experts.

C#, Generics and casting

Dear All,

I've a question related to Generics and casting in c# 2.0.
I've a class called Client which implements interface IClient.
I'd like to do:

LinkedList<Client> clients;
public LinkedList<IClient> getClients()
{
return clients;
}

but I got a casting error at compile time (even if the casting is
explicitly given). How should I do to get my list of IClient ?

Regards,

Greg

Mar 6 '06 #1
4 2548
Just becase Client : IClient, it does not follow that List<Client> :
List<IClient>; as a consequence, you can't quite do what you want to here.

One option is to create a new linked list, and then populate that using
clients (not via the ctor, but manually). It really depends whether it is
critical that this is the *same list*, rather than the same contents. If so,
the only way is to declare clients as List<IClient> and cast appropriately
when you need an actual Client (rather than IClient). Or even better, just
work with IClient throughout.

The reason is that otherwise a caller could declare SomeClass : IClient,
call getClients(), and then add a SomeClass to the list; this would compile,
but would blow up at runtime as the actual clients variable cannot contain
SomeClass; better to blow up at compile time - like it is now.

Marc
Mar 6 '06 #2
Why don't you declare the list as LinkedList<IClient> instead?
--

Stoitcho Goutsev (100)

<kr********@gmail.com> wrote in message
news:11*********************@z34g2000cwc.googlegro ups.com...
Dear All,

I've a question related to Generics and casting in c# 2.0.
I've a class called Client which implements interface IClient.
I'd like to do:

LinkedList<Client> clients;
public LinkedList<IClient> getClients()
{
return clients;
}

but I got a casting error at compile time (even if the casting is
explicitly given). How should I do to get my list of IClient ?

Regards,

Greg

Mar 6 '06 #3
Stoitcho Goutsev (100) wrote:
Why don't you declare the list as LinkedList<IClient> instead?


I could (and that's what I did), but then when iterating in the list I
need to cast my IClient in Client, which is not very nice :)

Cheers,

Greg

Mar 7 '06 #4
> Stoitcho Goutsev (100) wrote:
Why don't you declare the list as LinkedList<IClient> instead?

I could (and that's what I did), but then when iterating in the list I
need to cast my IClient in Client, which is not very nice :)

Cheers,

Greg


Why do you need to cast to the class type? Why not implement an interface
that gives you access to the pieces you want?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Mar 7 '06 #5

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

Similar topics

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...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
2
by: Herby | last post by:
I need to define my own types and arrays of these types. These types are for the most part extensions of the built in types and need to provide all the basic operations of arithmetic and...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
8
by: Kris Jennings | last post by:
Hi, I am trying to create a new generic class and am having trouble casting a generic type to a specific type. For example, public class MyClass<Twhere T : MyItemClass, new() { public...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
3
by: psyCK0 | last post by:
Hi all! I have a problem of casting generics to their base type. In the code below I first define a BaseList class that can hold items of any type that inherits from BaseItem. I then define a...
8
by: Tony Johansson | last post by:
Hello! I have read that in practice, casting proved to be several times faster than using a generic. So the main reason to use generics is not that the performance is better because that's...
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.