Today I tested the following code in Visual Studio 2010 (.NET Framework version 4.0)
Type[] interfaces = typeof(int[]).GetInterfaces();
And I was shocked to find these two on the list:
System.Collections.Generic.IReadOnlyList`1 [[System.Int32, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]], mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934
System.Collections.Generic.IReadOnlyCollection`1 [[System.Int32, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]], mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934
I used these two interfaces before in environments with the 4.5+ framework installed and according to the documentation and of they were created for 4.5. This does not compile in my environment:
System.Collections.Generic.IReadOnlyList<int> list = new int[3];
The type or namespace name 'IReadOnlyCollection' does not exist in the namespace 'System.Collections.Generic' (do you miss the assembly link?)
When I try this:
int[] array = new int[3]; Type iReadOnlyCollection = Type.GetType("System.Collections.Generic.IReadOnlyCollection`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); int count = (int)iReadOnlyCollection.GetProperty("Count").GetValue(array, null);
count is 3, as expected. What's going on here?
Edit: I donβt think the framework 4.5 is installed on my computer:
Edit 2: Thanks @ScottChamberlain, it turned out that I installed it.