2

I want to pass around some values in an array that will always be a known size. I would like to define a class that represents this array of decimal values which could not be resized, would always have the same number of elements, and supports the [] array notation.

In c++ I could do an operator overloading for this - but I can't see how to do it in c#

To be clear - the use of the class would be something like:

MyValues values = new MyValues;
values[3] = 14;
values[7] = 10

.... And later

decimal aValue = values[2];

Suggestions?

  • 2
    What's wrong with `decimal[]`? – SLaks Mar 04 '10 at 17:10
  • you should avoid using decimal, it has poor performance – Andrey Mar 04 '10 at 17:11
  • @Andrey - could you provide some detail? – Mike Two Mar 04 '10 at 17:11
  • 2
    @Mike: `Decimal` is implemented in software, so it will be slower than `double`, which is done by the FPU. However, `decimal` is more precise. – SLaks Mar 04 '10 at 17:17
  • @Slaks - Thank you. I do know that. I was only trying to get Andrey's reasoning in case it was something else. I probably should have noted the trade off in my comment. – Mike Two Mar 04 '10 at 17:20
  • You'll want to take a look at the index operator. See this question: http://stackoverflow.com/questions/424669/how-do-i-overload-the-operator-in-c – Stephen Mar 04 '10 at 17:11

5 Answers5

13

You need to write an indexer, like this:

public decimal this[int index] {
    get { return data[index]; }
    set { data[index] = value; }
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
5

Use an indexer

public class MyValues {
    private readonly decimal[] numbers = new decimal[10];

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

You might want to add some bounds checking to provide better failiure messages. Also you probably do not want to hard code the array size.

Mike Two
  • 44,935
  • 9
  • 80
  • 96
  • You don't need to add bounds checking - the array already does. – SLaks Mar 04 '10 at 17:27
  • @SLaks - yes of course, but you might want to fail in a different way than the way the array will fail. The "Clearly" part is clearly too strong here. – Mike Two Mar 04 '10 at 18:46
3

Using an indexer, you could write a simple generic class like:

    public class FixedArray<T>
    {
        private T[] array;

        public int Length { get { return array.Length; } }

        public FixedArray (int size)
        {
            array = new T[size];
        }

        public T this[int index]
        {
            get { return array[index]; }
            set { array[index] = value; }
        }
    }
bruno conde
  • 47,767
  • 15
  • 98
  • 117
2

something like:

decimal this[int ind]
{
   get
   {
      return array[ind];
   }
   set
   {
      array[ind] = value;
   }
}
Andrey
  • 59,039
  • 12
  • 119
  • 163
1

Try ReadOnlyCollection class. Also be aware of the dangers of arrays, really good article here

openshac
  • 4,966
  • 5
  • 46
  • 77