Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm confused about the implementation of interfaces.

ICollection<T> has the property IsReadOnly according to MSDN[^]
-And-
Collection<T> impliments ICollection<T> according to MSDN[^]
-So-
I thought that Collection<T> would have the property IsReadOnly.
-However-
C#
Collection<string> testCollection = new Collection<string>();
Console.WriteLine(testCollection.IsReadOnly);

The above code gives the compiler error:
'System.Collections.ObjectModel.Collection<string>' does not contain a definition for 'IsReadOnly' and no extension method 'IsReadOnly' accepting a first argument of type 'System.Collections.ObjectModel.Collection<string>' could be found (are you missing a using directive or an assembly reference?)
-While-
XML
Collection<string> testInterface = new Collection<string>();
Console.WriteLine(((ICollection<string>)testInterface).IsReadOnly);

The above code works.
-Question-
I thought classes implementing interfaces had to implement every property, so why doesn't testCollection have the IsReadOnly property unless you cast it as ICollection<string>?
Posted
Comments
Pheonyx 2-Aug-13 11:00am    
What usings do you have at the top of your file? I suspect you might be missing one.

1 solution

The IsReadOnly[^] property is implemented explicitely. That means you can only call this property when the object is explicitly cast to an ICollection<t>.

More info about explicit interface implementation can be found here[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900