473,799 Members | 2,693 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 4534
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
7910
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
1325
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
2385
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
5898
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
5120
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
9550
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
10269
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...
1
10248
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
10032
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...
1
7573
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
6811
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
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2942
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.