36

I've been trying this a few different ways, but I'm reaching the conclusion that it can't be done. It's a language feature I've enjoyed from other languages in the past. Is it just something I should just write off?

Kilhoffer
  • 32,375
  • 22
  • 97
  • 124

3 Answers3

60

No, static indexers aren't supported in C#. Unlike other answers, however, I see how there could easily be point in having them. Consider:

Encoding x = Encoding[28591]; // Equivalent to Encoding.GetEncoding(28591)
Encoding y = Encoding["Foo"]; // Equivalent to Encoding.GetEncoding("Foo")

It would be relatively rarely used, I suspect, but I think it's odd that it's prohibited - it gives asymmetry for no particular reason as far as I can see.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Exactly. They have their place. Others on here talking about design issues clearly have never worked with a classic language that allowed, since they are very helpful. – user9991 Sep 30 '08 at 19:20
  • 1
    Yep. My point exactly. They have a very useful place in architecture. Some people have their blinders on, I guess! – Kilhoffer Sep 30 '08 at 19:23
  • 5
    Crap. Now I gotta write a static method Cache.Get(key) instead of Cache[key]... – Gishu Feb 16 '10 at 05:12
  • 6
    When are we as a community going to realise that it is impossible to predict how people put some tool to use. I'd love this feature in C#. – Thiru Aug 05 '11 at 19:36
  • 1
    +1 this answer every time you need them! – cerkiewny Apr 17 '14 at 10:57
16

You can simulate static indexers using static indexed properties:

public class MyEncoding
{
    public sealed class EncodingIndexer
    {
        public Encoding this[string name]
        {
            get { return Encoding.GetEncoding(name); }
        }

        public Encoding this[int codepage]
        {
            get { return Encoding.GetEncoding(codepage); }
        }
    }

    private static EncodingIndexer StaticIndexer;

    public static EncodingIndexer Items
    {
        get { return StaticIndexer ?? (StaticIndexer = new EncodingIndexer()); }
    }
}

Usage:

Encoding x = MyEncoding.Items[28591]; // Equivalent to Encoding.GetEncoding(28591)   
Encoding y = MyEncoding.Items["Foo"]; // Equivalent to Encoding.GetEncoding("Foo")   
Giorgi Chakhidze
  • 3,351
  • 3
  • 22
  • 19
  • 3
    Although Jon's answer (as usual) is most correct this isn't such a bad alternative for some situations. – Thiru Aug 05 '11 at 19:32
0

No, but it is possible to create a static field that holds an instance of a class that uses an indexer...

namespace MyExample {

   public class Memory {
      public static readonly MemoryRegister Register = new MemoryRegister();

      public class MemoryRegister {
         private int[] _values = new int[100];

         public int this[int index] {
            get { return _values[index]; }
            set { _values[index] = value; }
         }
      }
   }
}

...Which could be accessed in the way you are intending. This can be tested in the Immediate Window...

Memory.Register[0] = 12 * 12;
?Memory.Register[0]
144
dynamichael
  • 807
  • 9
  • 9