How to use SortedList.Item Property
-
Hi, Can somebody please tell me how to use the "Item" property in System.Collections.SortedList class. I wanted to get and set the value field depending on the key. Thanks Raj
-
Hi, Can somebody please tell me how to use the "Item" property in System.Collections.SortedList class. I wanted to get and set the value field depending on the key. Thanks Raj
-
Hi, Can somebody please tell me how to use the "Item" property in System.Collections.SortedList class. I wanted to get and set the value field depending on the key. Thanks Raj
If you look at the documentation for the
Items
property, it says that it's the indexer in C# (the same as other lists'Items
properties). This means you use it like this:myList[key] = value;
value = myList[key];You can use a key because a
SortedList
implementIDictionary
, an interface for key/value collections. Mosts lists in the .NET FCL simply take an index (0-based, as with all .NET languages) in their indexer.Microsoft MVP, Visual C# My Articles
-
If you look at the documentation for the
Items
property, it says that it's the indexer in C# (the same as other lists'Items
properties). This means you use it like this:myList[key] = value;
value = myList[key];You can use a key because a
SortedList
implementIDictionary
, an interface for key/value collections. Mosts lists in the .NET FCL simply take an index (0-based, as with all .NET languages) in their indexer.Microsoft MVP, Visual C# My Articles
Thanks for your reply. I am new to .Net. Trying to understand these properties. DO I have to override this property. I am doing like this, but it doesn't work. In my Sorted list keys are of type string and value is of type int. public override int Item(string myKey) { get { return mySrotedList[myKey]; } set { mySortedList[myKey] = value; } } Thanks for your help and time. Raj
-
Thanks for your reply. I am new to .Net. Trying to understand these properties. DO I have to override this property. I am doing like this, but it doesn't work. In my Sorted list keys are of type string and value is of type int. public override int Item(string myKey) { get { return mySrotedList[myKey]; } set { mySortedList[myKey] = value; } } Thanks for your help and time. Raj
Why are you trying to override this? If you're just using a
SortedList
, you just use it like the code example I gave. If you do want to extend a class, you only have to overrideabstract
methods, and override other methods and properties as you need to. In this case, though, it's seems like you don't need to. Besides, when you declare an indexer for a class in C#, you use the following syntax:public object this[int index]
{
get { return internalList[index]; }
set { internalList[index] = value; }
}Read the .NET Framework SDK for more information, especially since your new. Specifically, see the Visual C# Language[^] topic.
Microsoft MVP, Visual C# My Articles