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

simple generics IList question

Hi

I have never used generics before, and I was wondering if the following sort
of use was acceptable/normal for a method:

public IList<IPerson> GetPersons()
{
IList<IPerson> personList = new List<IPerson>();
... // get the persons
return personList;
}
The meaning is that I return a "List" which holds only "IPerson" objects. In
earlier projects I have made without generics, I would have returned a
normal IList (eg ArrayList) or an array IPerson[].
Then I think the client should call it like:

IList<IPerson> persons = GetPersons();
Thanks,
Peter
Apr 12 '06 #1
1 1872
Peter Kirk wrote:
I have never used generics before, and I was wondering if the following sort
of use was acceptable/normal for a method:

public IList<IPerson> GetPersons()
{
IList<IPerson> personList = new List<IPerson>();
... // get the persons
return personList;
}

The meaning is that I return a "List" which holds only "IPerson" objects. In
earlier projects I have made without generics, I would have returned a
normal IList (eg ArrayList) or an array IPerson[].

Then I think the client should call it like:

IList<IPerson> persons = GetPersons();


Yes, that's absolutely fine, and a very normal way of using generics.

Warning: relatively complex stuff follows. If you're happy with just
the above, feel free to skip the stuff below. If you want to know more
about generics for the sake of interest, however, this may be useful.

One thing you *may* be surprised about is that you can't do:

IList<IPerson> list = new List<Person>();

where Person implements IPerson. C# generics doesn't have the concept
of covariance/contravariance (yet). (With covariance/contravariance
you'd need to have slightly different syntax to the above in order to
say it was a "list of some unspecified type which implements IPerson".
That would have to prevent you from adding new objects (as they might
not be of the right type) but would still allow objects to be fetched
as IPerson.)

Jon

Apr 12 '06 #2

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

Similar topics

13
by: Sherif ElMetainy | last post by:
Hello I was just got VS 2005 preview, and was trying generics. I tried the following code int intArray = new int; IList<int> intList = (IList<int>) intArray; it didn't compile, also the...
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: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a...
2
by: rajivpopat | last post by:
I've been reading a discussion thread at http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/119f8362a9f5ff52 regarding typecasting generic collections to...
6
by: russ | last post by:
Hi, We have stumbled across an issue using the type safe collection System.Collections.ObjectModel.Collection <T> to retrieve data from our data layer. Say we have a customer object and want...
19
by: Fernando Cacciola | last post by:
I'm puzzled, Why and how _exactly_ is this: void Foo<T>(T v ) where T : Interface/Value/Class/class any better than this void Foo( Interface/Value/Class/object v )
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
20
by: -- | last post by:
Imagine I have a class TypeX and a class TypeY that inherts TypeX. public class typeX { .... } public class typeY : typeX { ....
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.