Development Tip

모든 배열이 C #에서 구현하는 인터페이스는 무엇입니까?

yourdevel 2020. 11. 6. 20:39
반응형

모든 배열이 C #에서 구현하는 인터페이스는 무엇입니까?


새로운 .NET 3.5 프로그래머로서 저는 LINQ를 배우기 시작했고 이전에 알아 차리지 못했던 매우 기본적인 것을 발견했습니다.

이 책은 모든 배열이 구현한다고 주장합니다 IEnumerable<T>(분명히 그렇지 않으면 배열에서 LINQ to 개체를 사용할 수 없습니다 ...). 나는이를봤을 때, 난 정말 그것에 대해 생각하지 않을 것을 자신에게 생각, 나는 모든 배열 구현 밖의 무엇을 나 자신에게 물었다 - 나는 시험 때문에 System.Array에, (가 CLR의 모든 배열의 기본 클래스가 이후) 개체 브라우저를 사용하여 놀랍게도 IEnumerable<T>.

그래서 내 질문은 : 정의는 어디에 있습니까? 내 말은, 모든 배열이 구현하는 인터페이스를 정확히 어떻게 알 수 있습니까?


로부터 문서 (강조 광산) :

[...] Array 클래스 구현 System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>System.Collections.Generic.IEnumerable<T>일반 인터페이스. 구현은 런타임에 배열에 제공되므로 문서 빌드 도구에 표시되지 않습니다.

편집 : Jb Evain이 그의 의견에서 지적했듯이 벡터 (1 차원 배열) 만 일반 인터페이스를 구현합니다. 에 관해서는 다차원 배열은 일반적인 인터페이스를 구현하지 않습니다, 나는 확신가 아닌 일반적인 대응을 시행하고 있기 때문에 아니에요 (아래 클래스 선언 참조).

System.Array클래스 (즉, 모든 배열) 이러한 제네릭이 아닌 인터페이스를 구현 :

public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable

작은 코드 조각을 사용하여 경험적으로 질문에 대한 답을 찾을 수 있습니다.

foreach (var type in (new int[0]).GetType().GetInterfaces())
    Console.WriteLine(type);

위의 스 니펫을 실행하면 다음과 같은 출력이 생성됩니다 (on .NET 4.0).

System.ICloneable
System.Collections.IList
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IStructuralComparable
System.Collections.IStructuralEquatable
System.Collections.Generic.IList`1[System.Int32]
System.Collections.Generic.ICollection`1[System.Int32]
System.Collections.Generic.IEnumerable`1[System.Int32]

( `1의미 <T>)

.NET 4.5( .NET Standard 1.0이상), 두 개의 추가 인터페이스가있다 :

System.Collections.Generic.IReadOnlyList`1[System.Int32]
System.Collections.Generic.IReadOnlyCollection`1[System.Int32]

.NET 4.5 부터 배열은 인터페이스 System.Collections.Generic.IReadOnlyList<T>System.Collections.Generic.IReadOnlyCollection<T>.

따라서 .NET 4.5를 사용할 때 배열로 구현 된 인터페이스의 전체 목록은 다음과 같습니다 ( Hosam Aly의 답변에 제시된 방법을 사용하여 얻음 ).

System.Collections.IList
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IStructuralComparable
System.Collections.IStructuralEquatable
System.Collections.Generic.IList`1[System.Int32]
System.Collections.Generic.ICollection`1[System.Int32]
System.Collections.Generic.IEnumerable`1[System.Int32]
System.Collections.Generic.IReadOnlyList`1[System.Int32]
System.Collections.Generic.IReadOnlyCollection`1[System.Int32]

이상하게도이 두 인터페이스를 언급하기 위해 MSDN문서 를 업데이트하는 것을 잊은 것 같습니다 .


배열 인터페이스에서 조심스럽게 구현할 수 있지만 실제로는이를 수행하지 않습니다. 다음 코드를 살펴보십시오.

            var x = new int[] { 1, 2, 3, 4, 5 };
        var y = x as IList<int>;
        Console.WriteLine("The IList:" + string.Join(",", y));
        try
        {
            y.RemoveAt(1);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        Console.WriteLine(string.Join(",", y));

It produces the following output: result

So parsing works but not everything is supported which is correct from fixed length collection perspective but quite wrong if you really believe that it is a list. There goes Liskov principle from SOLID :(.

For testing fast this will help.


I have found the implementation of the IList<T>, ICollection<T>, IEnumerable<T> in the SZArrayHelper nested class of the Array.

But i have to warn you - there you will find much more questions...

The refference

After that i got only one - there_is_no_array ;)

참고URL : https://stackoverflow.com/questions/4482557/what-interfaces-do-all-arrays-implement-in-c

반응형