Phil
Phil

Reputation: 6679

Where is List<T>.IsReadOnly?

In the .Net Framework, List<T> implements the ICollection<T> interface. However when looking at the List class in Visual Studio, I see no IsReadOnly property, which is supposedly in the ICollection<T> interface.

How is it possible for a class to implement an interface... but not really implement it?

Upvotes: 2

Views: 101

Answers (3)

Guffa
Guffa

Reputation: 700382

It's made using an explicit interface implementation. You can only see the implementation when you use the list as that specific interface:

List<int> x = new List<int>();

bool b1 = x.IsReadOnly; // not accessible

ICollection<T> y = x;

bool b2 = y.IsReadOnly; // accessible

Upvotes: 1

Brian Rasmussen
Brian Rasmussen

Reputation: 116411

IsReadOnly is listed under the Explicit Interface Implementations section of the documentation.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500903

It uses explicit interface implementation. For example:

public interface IFoo 
{
    void Bar();
}

public Foo : IFoo
{
    // Note the lack of public here
    void IFoo.Bar() {}
}

Upvotes: 5

Related Questions