473,811 Members | 3,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does ReadOnlyCollect ion<T> implement IList<T> ?

Does ReadOnlyCollect ion<T> really implement IList<T>, like it claims
to do? ...
When I right-click on ReadOnlyCollect ion and look at its definition,
it says this:

public class ReadOnlyCollect ion<T>
: IList<T>,IColle ction<T>,IEnume rable<T> ...

However, the following code fails its assertion check:

List<string> xs = new List<string>();
ReadOnlyCollect ion<string> ro = new ReadOnlyCollect ion<string>(xs) ;
IList<string> il = ro as IList<string>;
Debug.Assert(il != null);

I don't understand how this is possible. If ReadOnlyCollect ion<T>
claims to implement IList<T>, then I should be able to cast to it,
shouldn't I?
In a similar vein, the following code fails to compile because, it
claims, ReadOnlyCollect ion<string> doesnt implement the Insert method.
How is this possible? Insert is part of IList<T>, which
ReadOnlyCollect ion<T> inherits from, so it should be there!

ro.Insert(0,"he llo");

I'm asking these questions because I want to create my own
wrapper-class, similar to how ReadOnlyCollect ion is a wrapper around
an IList. I need to understand which interfaces I should declare
myself as providing. And so I need to understand why
ReadOnlyCollect ion declares that it supports the interfaces it does,
and how.

I wonder if ReadOnlyCollect ion really does implement those interfaces,
but somehow *hides* the Insert method &c.? How is this done in C#? The
documentation specifically says that methds from interfaces must be
public, so I don't know.
--
Lucian
Apr 1 '06 #1
2 4535
Lucian Wischik <lu***@wischik. com> wrote:
Does ReadOnlyCollect ion<T> really implement IList<T>, like it claims
to do? ...
When I right-click on ReadOnlyCollect ion and look at its definition,
it says this:

public class ReadOnlyCollect ion<T>
: IList<T>,IColle ction<T>,IEnume rable<T> ...

However, the following code fails its assertion check:

List<string> xs = new List<string>();
ReadOnlyCollect ion<string> ro = new ReadOnlyCollect ion<string>(xs) ;
IList<string> il = ro as IList<string>;
Debug.Assert(il != null);
Could you post a short but complete program that demonstrates that?
Here's one which seems to go against it:

using System;
using System.Collecti ons.Generic;
using System.Collecti ons.ObjectModel ;

class Test
{
static void Main()
{
List<string> xs = new List<string>();
ReadOnlyCollect ion<string> ro =
new ReadOnlyCollect ion<string>(xs) ;
IList<string> il = ro as IList<string>;
Console.WriteLi ne (il != null);
}
}

That prints out "True" as expected.
In a similar vein, the following code fails to compile because, it
claims, ReadOnlyCollect ion<string> doesnt implement the Insert method.
How is this possible? Insert is part of IList<T>, which
ReadOnlyCollect ion<T> inherits from, so it should be there!

ro.Insert(0,"he llo");
No, it shouldn't - ReadOnlyCollect ion implements IList<T>.Insert
explicitly, so it's only "there" when you're using the
ReadOnlyCollect ion as an IList<T>. In other words, you can do:

((IList<T>)ro). Insert (0, "hello");
I wonder if ReadOnlyCollect ion really does implement those interfaces,
but somehow *hides* the Insert method &c.? How is this done in C#? The
documentation specifically says that methds from interfaces must be
public, so I don't know.


Search for "explicit interface implementation" . Basically, instead of:

public void MethodName(...) ;

you do

void InterfaceName.M ethodName(...);

--
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
Apr 1 '06 #2
Jon Skeet [C# MVP] <sk***@pobox.co m> wrote:
... Search for "explicit interface implementation" .
Okay, I understand now. Thanks for the prompt and helpful answer.

Could you post a short but complete program that demonstrates that?


No -- it was a mistake on my part, caused by stopping the debugger AT
the cast rather than after it.

--
Lucian
Apr 1 '06 #3

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

Similar topics

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...
1
4716
by: Michael Primeaux | last post by:
Why does the generic SortedList and generic SortedDictionary not define any virtual members? Thanks, Michael
4
5718
by: Dave Booker | last post by:
So did the .NET 2.0 working group just run out of steam before it got to a ReadOnlyDictionary<> wrapper? I am publishing events containing a SortedList<>, and obviously I don't want event handlers to be able to do anything to the collection at the same time other recipients could be handling it. I found a ReadOnlyCollection<> wrapper, which I can apply to the .Keys and ..Values of the SortedList, but I can't find any way to wrap the...
6
6007
by: nicolas.rolland | last post by:
Would anyone know the reson why IList<Tdoes not implements IList ?? This results in strange behaviours, like typeof(IList).IsAssignableFrom(typeof(List<string>)) --true typeof(IList).IsAssignableFrom(typeof(IList<string>)) --false
9
7913
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
0
1326
by: submicron | last post by:
Hi All, I'm having problems implementing a readonly interface to a set of classes. Heres a simplification of the code: interface IreadonlyBase { // Does gets only on base class } class baseclass : IreadonlyBase
4
2387
by: Anastasios Hatzis | last post by:
I'm looking for a pattern where different client implementations can use the same commands of some fictive tool ("foo") by accessing some kind of API. Actually I have the need for such pattern for my own tool (http://openswarm.sourceforge.net). I already started restructuring my code to separate the actual command implementations from the command-line scripts (which is optparser-based now) and have some ideas how to proceed. But probably...
35
5900
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:
36
5127
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT do so, since this is not part of the specification. How then to you apply styles to entire columns? Surely you don't have to write <td class="behold"on every row item.
0
9731
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
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10393
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
10136
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
9208
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
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
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
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
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.