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

Home Posts Topics Members FAQ

IList and IList<>

Would anyone know the reson why IList<Tdoes not implements IList ??
This results in strange behaviours, like

typeof(IList).I sAssignableFrom (typeof(List<st ring>)) --true
typeof(IList).I sAssignableFrom (typeof(IList<s tring>)) --false

Sep 9 '06 #1
6 6005
<ni************ *@gmail.coma écrit dans le message de news:
11************* ********@e3g200 0c...legrou ps.com...

| Would anyone know the reson why IList<Tdoes not implements IList ??

Because, if it did, you would end up with an interface that had two Item,
two Add, two Contains, etc. properties and methods.

IList deals with all list operations as System.Object, IList<Tdeals with
all the same operations as strictly typed to the type of T.

Interface inheritance is not always as appropriate as class inheritance.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 9 '06 #2
Joanna Carter [TeamB] <jo****@not.for .spamwrote:
<ni************ *@gmail.coma écrit dans le message de news:
11************* ********@e3g200 0c...legrou ps.com...

| Would anyone know the reson why IList<Tdoes not implements IList ??

Because, if it did, you would end up with an interface that had two Item,
two Add, two Contains, etc. properties and methods.
Just as IEnumerator<Tdo es... I suspect there's a good reason for the
inconsistency there, but it's present nonetheless :(
IList deals with all list operations as System.Object, IList<Tdeals with
all the same operations as strictly typed to the type of T.

Interface inheritance is not always as appropriate as class inheritance.
And class inheritance isn't always used appropriately to start with ;)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 9 '06 #3
"Jon Skeet [C# MVP]" <sk***@pobox.co ma écrit dans le message de news:
MP************* ***********@msn ews.microsoft.c om...

| Just as IEnumerator<Tdo es... I suspect there's a good reason for the
| inconsistency there, but it's present nonetheless :(

Now, I do find it useful to have both versions there :-)

| And class inheritance isn't always used appropriately to start with ;)

Hmmm, didn't you know the world can be modelled using nothing other than
inheritance ? <gd&r>

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 9 '06 #4
>
Interface inheritance is not always as appropriate as class inheritance.
The way I see interface is that it is a synonym for a "contract"

That by the way, is why you can't have a public modifier keyword when
explicitly implementing an interface. because you are not supposed to
access directly form an object a interface member. aka you have to read

toto.interfacem ember()
as a shortcut for
(toto as interface).inte rfacemember()
So, following that notion of contract, i see no reason why a IList<T>
contract does not fullfill a IList contract.

I agree that would be too much code though so it might not be
practically appropriate
I guess this makes a case for a form of dynamic interface.

Sep 12 '06 #5
<ni************ *@gmail.coma écrit dans le message de news:
11************* *********@i42g2 00...legr oups.com...

| The way I see interface is that it is a synonym for a "contract"

Agreed

| So, following that notion of contract, i see no reason why a IList<T>
| contract does not fullfill a IList contract.

MY thought is that the hierarchies, non-generic and generic are split
differently and could not be reconciled because it would break backwards
compatibility.

public interface ICollection : IEnumerable
{
int Count { get; }
bool IsSynchronized { get; }
object SyncRoot { get; }
void CopyTo(Array array, int index);
}
public interface IList : ICollection, IEnumerable
{
bool IsFixedSize { get; }
bool IsReadOnly { get; }
object this[int index] { get; set; }
int Add(object value);
void Clear();
bool Contains(object value);
int IndexOf(object value);
void Insert(int index, object value);
void Remove(object value);
void RemoveAt(int index);
}

public interface ICollection<T: IEnumerable<T>, IEnumerable
{
int Count { get; }
bool IsReadOnly { get; }
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
}

public interface IList<T: ICollection<T>, IEnumerable<T>, IEnumerable
{
T this[int index] { get; set; }
int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
}

As you can see, the members of IList<Tare not just typesafe equivalents of
IList, the members are split differently between ICollection and IList.

The question would be raised, which is the bettter split of functionality ?

One other point : why do the List interfaces inherit from IEnumerable again,
when inheriting from ICollection would already give this functionality.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 12 '06 #6
<ni************ *@gmail.comwrot e:

<snip>
So, following that notion of contract, i see no reason why a IList<T>
contract does not fullfill a IList contract.
IList has the Add(object) method. Unless the object happens to be of the
right type, that would have to fail - basically you lose compile-time
type safety, which is meant to be the point of generics.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #7

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

Similar topics

13
2713
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 following didn't compile
4
25413
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 following error: Argument '1': cannot convert from 'GenericsPOC.ArticleCollection' to 'System.Collections.Generic.IList<object>' I tried casting specifically to IList<object> but then I get a runtime error: Unable to cast object of type...
5
13643
by: Lonifasiko | last post by:
I've got this in my code, that is, we're inside a class called Patient, so a Patient can have Appointments: private IList<Appointment> _appointments = null; public IList<Appointment> Appointments { get
4
3903
by: Mark | last post by:
I have a COM object that calls into a C# Forms library. The library can throw exceptions and I want to handle the exceptions in COM. Adam Nathan wrote a Microsoft sponsored book titled .Net and Com the complete interoperability guide which shows examples of how to do this. Below please find the book example code for getting extended error information from a V-Table bound .Net component. This code does not compile because the compiler gives...
5
1922
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() { pageSize = (this.Count == 0) ? 1 : this.Count; pageNumber = 1; }
2
4533
by: Lucian Wischik | last post by:
Does ReadOnlyCollection<T> really implement IList<T>, like it claims to do? ... When I right-click on ReadOnlyCollection and look at its definition, it says this: public class ReadOnlyCollection<T> : IList<T>,ICollection<T>,IEnumerable<T> ...
1
4716
by: Michael Primeaux | last post by:
Why does the generic SortedList and generic SortedDictionary not define any virtual members? Thanks, Michael
2
1538
by: Tom | last post by:
Hi, I have a simple Product Domain Entity class with just fields/properties. I have a IList<Productthat I need to search for a particular Product object based on a field/property ProductID. The only solution I could come up with is a method that loops through the IList<Product(foreach) and looks at each ProductID and returning the Product with the matching ProductID. Is this the best solution? Thanks
35
5895
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to perform the same operation in one fell swoop on a List<>. For example:
0
9656
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...
1
10110
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
9967
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
8993
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
7517
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
6750
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();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
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.