Question

I would like to create a SortedList with just keys of type Integer, but I'm finding it difficult to set them myself considering the nature of the code.

Is there any way for the list to generate the keys themselves? Like, an autonumber sort of thing? This may be obvious but I've been Googling away and can't find anything!

Thanks in advance.

Was it helpful?

Solution

Why not use a standard List<T>? They are sorted by an index, which is always incremented.

OTHER TIPS

You could wrap SortedList around with a class defined by you, that could do the extra book keeping.

An autonumber (or IDENTITY) is really just a thread-safe counter, so you could just create a property or class level variable that retrieves the next value and then increments that value:

class TestAutoIncrement
    {
        int counter = 0;
        private Object thisLock = new Object();

        public int GetNextCounterValue()
        {
            lock (thisLock)
            {
                counter++;
                return counter;
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top