Vector start with element greater than 0 (zero)?

3

Usually a vetor starts from element 0 , and so on until it reaches the size of which it was declared or assigned, eg:

string[] v = {1, 2 , 3};
// equivalente á:
v[0] = 1;
v[1] = 2;
v[2] = 3;

I want to know how to do in C# to start the vector already in a pre-defined element, that is, instead of starting with v[0] start in v[10] like this:

v[10] = 1;
v[11] = 2;
v[12] = 3;
    
asked by anonymous 13.01.2016 / 19:21

1 answer

6

In C # I only see one way, create a structure of your own that does it for you. You can not use anything that already exists directly, array , List , nothing. Everything starts from scratch. It might have some ready structure that works differently, but I do not remember anything standard and I doubt you have it in .Net.

Simplified example of what you can have in your own type:

public class MyList<T> : List<T> {
    public T this[int index] {
        get {
            return base[index - 10];
        }
        set {
            base[index - 10] = value;
        }
    }
}

I found a answer in SO with a more complete implementation on the one hand, more limited on the other.

I found this answer in the SO that gives another solution, but it's really bad to use that.

Another solution that is not ideal: you can simply leave the first 10 elements empty. Make a calculation before using the index. Resolve, but not transparently.

I would simply avoid this. Perhaps with a more comprehensive description of the problem, the solution should be another.

    
13.01.2016 / 19:39