c# - Hardcoded index for array access -
I have an array in which the data is in a specific order, I usually encode the index to not be too confused. :
public class MyClass {private redone String [] data = new string [DATA_LENGHT]; Internal cone INDEX_NAME = 00; Internal content INDEX_FIRSTNAME = 01; Internal content INDEX_CITY = 02; ... internal constate DATA_LENGHT = XX; }
But to maintain this way is boring. If I want to put a new data on index 0, then I have to manually replace all the later index xx.
I would like to use some kind of initiator, but will not work below:
Internal condom INDEX_NAME = i ++; Internal command INDEX_FIRSTNAME = i ++; Internal content INDEX_CITY = i ++;
I can convert Constast to read only and start it in a stable constructor, but it means that the initialization of two lines for initialization (does not look really good ).
What would be a clean, easy way to do this?
The best answer is probably suggested as a enum
.
enum index: int {name = 0, first name, city, // ... length}
Another solution will increase the previous value .
Internal const int INDEX_NAME = 0; Internal const int INDEX_FIRSTNAME = INDEX_NAME + 1; Internal const int INDEX_CITY = INDEX_FIRSTNAME + 1;
Comments
Post a Comment