Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# pattern matching – type test array

The doco on using pattern matching for type testing states:

Another common use for pattern matching is to test a variable to see if it matches a given type. For example, the following code tests if a variable is non-null and implements the System.Collections.Generic.IList interface. If it does, it uses the ICollection.Count property on that list to find the middle index. The declaration pattern doesn’t match a null value, regardless of the compile-time type of the variable. The code below guards against null, in addition to guarding against a type that doesn’t implement IList.

https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching#type-tests

Why is it that in the following example new int[] { 1 } passes as an IList<T> ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ListExample(new List<int> { 1 }); // Sequence is IList<T>
ListExample(null as IList<int>); // Sequence was null.
ListExample(new int[] { 1 }); // Sequence is IList<T> ??

public static void ListExample<T>(IEnumerable<T> sequence)
{
    if (sequence is IList<T> list)
    {
        Console.WriteLine("Sequence is IList<T>");
    }
    else if (sequence is null)
    {
        Console.WriteLine("Sequence was null.");
    }
    else
    {
        Console.WriteLine("Sequence was other type of IEnumerable<T>");
    }
}

I cross referenced the is / as operator doco:

These operators and expressions perform type checking or type conversion. The is operator checks if the run-time type of an expression is compatible with a given type. The as operator explicitly converts an expression to a given type if its run-time type is compatible with that type.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast

It sounds like it has nothing to do with whether or not the type implements the interface, just whether it can be cast as the target type?

>Solution :

Because int[] actually implements this interface. From the System.Array docs:

Single-dimensional arrays implement the System.Collections.Generic.IList<T>, System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyList<T> and System.Collections.Generic.IReadOnlyCollection<T> generic interfaces. The implementations are provided to arrays at run time, and as a result, the generic interfaces do not appear in the declaration syntax for the Array class.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading