Home >Backend Development >C++ >How Do C# Arrays Seemingly Implement IList Without an Explicit Count Property?
How Arrays Partially Implement IList
In C#, arrays implement IList
Delving into the Framework
While arrays do not implement IList
CLR's Quacks-Like-a-Duck Approach
The CLR implements special techniques for arrays, similar to those used for value types. The compiler recognizes casts to IList
Count Property Implementation
Despite not being explicitly declared, the Count property can be accessed. However, it is implemented as follows:
internal int get_Count<T>() { // Warning: "this" is an array, not an SZArrayHelper T[] _this = JitHelpers.UnsafeCast<T[]>(this); return _this.Length; }
As the comment suggests, this behavior may be considered a rule violation, but it facilitates efficient array handling.
Conclusion
Arrays partially implement IList
The above is the detailed content of How Do C# Arrays Seemingly Implement IList Without an Explicit Count Property?. For more information, please follow other related articles on the PHP Chinese website!