Overloaded enumerator: Cannot use default indexer

Given the following code:

namespace MyApp
{
    using System;
    using System.Collections.ObjectModel;

    class Program
    {
        static void Main(string[] args)
        {
            var col = new MyCollection();
            col.Add(new MyItem { Enum = MyEnum.Second });
            col.Add(new MyItem { Enum = MyEnum.First });

            var item = col[0];
            Console.WriteLine("1) Null ? {0}", item == null);

            item = col[MyEnum.Second];
            Console.WriteLine("2) Null ? {0}", item == null);

            Console.ReadKey();
        }
    }

    class MyItem { public MyEnum Enum { get; set; } }

    class MyCollection : Collection<MyItem>
    {
        public MyItem this[MyEnum val]
        {
            get
            {
                foreach (var item in this) { if (item.Enum == val) return item; }
                return null;
            }
        }
    }

    enum MyEnum
    {
        Default = 0,
        First,
        Second
    }
}

I was surprised to see the following result:

1) Null ? True
2) Null ? False

My first expectation was that, as I was passing int, the default index should be used, and the first call should be successful.

Instead, it seems that overload waiting enumis always called (even when casting 0 as an int), and the test fails.

  • Can someone explain this behavior to me?
  • And give a workaround to support two indexers: one by index and one for listing?

EDIT . The workaround seems to distinguish the collection as Collection, see this answer .

So:

  1. "" ( , )? int int? ( , )

:

  • 0 .
  • , .

( ) . :

+5
3

, enum int -, enum int , , .

( : , const int 0 enum int , , : MyCollection.)

, , int Collection<T> - , , .

, int , :

public class MyCollection : Collection<MyItem>
{
    public new MyItem this[int index]
    {
            // make sure we get Collection<T> indexer instead.
        get { return base[index]; }
    }
}

, 0 enum:

13.1.3. . - 0 .

,

        int index = 0;
        var item = col[index];

, int, :

        var item = col[1];
        Console.WriteLine("1) Null ? {0}", item == null);

, 1 enum

- , , Collection<T> . , , enum , 0 int .

, -, 7.4.2 Overload Resolution , :

, -

, , , .

+5

# 0 . , . , . , :

int x = 0;
var item = col[x];

, x 0 , this[int value]. ( : -))

# 0 . 3.0, , 0, . (int)0 .

:

, , , , . , , :

public class Test
{
    public void Print(int number)
    {
        Console.WriteLine("Number: " + number);
    }

    public void Print(Options option)
    {
        Console.WriteLine("Option: " + option);
    }
}

public enum Options
{
    A = 0,
    B = 1
}

:

t.Print(0); // "0"
t.Print(1); // "1"
t.Print(Options.A); // "A"
t.Print(Options.B); // "B"

, Print(int) , Print(Options) :

public class TestBase
{
    public void Print(int number)
    {
        Console.WriteLine("Number: " + number);
    }
}

public class Test : TestBase
{
    public void Print(Options option)
    {
        Console.WriteLine("Option: " + option);
    }
}

:

t.Print(0); // "A"
t.Print(1); // "1"
t.Print(Options.A); // "A"
t.Print(Options.B); // "B"
+2
source

All Articles