Question:
Arrays in C# implement IList
Answer:
Arrays and Generic Interfaces
Arrays in C# implement System.Collections.IList, which is not generic. They do not directly implement System.Collections.Generic.IList
Hidden Implementation in CLR
However, the Common Language Runtime (CLR) creates concrete array types dynamically. These types internally use the System.SZArrayHelper class to implement the IList
Compiler and CLR Knowledge
The compiler and the CLR are aware of array types and understand how to cast them to IList
Missing Count Property
The Count property is not explicitly implemented. Instead, the CLR internally uses a method that maps to the Length property of the array:
internal int get_Count<T>() { T[] _this = JitHelpers.UnsafeCast<T[]>(this); return _this.Length; }
Breaking the Rules?
This internal implementation arguably violates the strict interface implementation rules. However, it provides a convenient and seamless way for arrays to behave as if they implemented IList
Additional Insights
The above is the detailed content of Do C# Arrays Truly Implement the `IList` Interface Despite Lacking a Public `Count` Property?. For more information, please follow other related articles on the PHP Chinese website!