SoFunction
Updated on 2025-02-28

Introduction to the container by C# key value

StringDictionary: The default key is case-insensitive
NameValueCollection: The default key is case sensitive
KeyedCollection: It is not a key-value-to-container, but it is better than a key-to-container. It is highly recommended

Namespace using

A namespace contains interfaces and classes that define a collection of various objects (such as lists, queues, bit arrays, hash tables, and dictionaries).
The namespace contains interfaces and classes that define generic collections. Generic collections allow users to create strongly typed collections, which provide better type safety and performance than non-generic strongly typed collections.
Namespaces contain dedicated and strongly typed collections, such as linked list dictionaries, bit vectors, and collections containing only strings.

Hashtable、SortedList
SortedList is a sortable dictionary. When adding elements, the elements are inserted into the SortedList in the correct sort order. At the same time, the index is automatically adjusted accordingly, and the same is true for removing elements.
The keys and values ​​of Hashtable and SortedList are both object types, so when used, the conversion is more frequent.

dictionary
Generic Dictionary, you can specify the type of key and value at will

Copy the codeThe code is as follows:

Dictionary <String, String> dic = new Dictionary <string, string> ();
( "1 ", "Jerry ");
( "2 ", "Kimmy ");
( "3 ", "Tommy ");

 

You can also define the class yourself to use

Copy the codeThe code is as follows:

public class KeyValueItem
    {
        private int _Value;
        public int Value
        {
            get
            {
                return _Value;
            }
        }
        private string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
        }
        //
        public KeyValueItem(string name, int value)
        {
            _Name = name;
            _Value = https:///dgjack/archive/2012/03/03/value;
        }
        public override string ToString()
        {
            return _Name;
        }
    }

When inserting values:
Copy the codeThe code is as follows:

KeyValueItem it = new KeyValueItem("Customer1", 1);
            (it);
it = new KeyValueItem("Customer2", 2);
            (it);
it = new KeyValueItem("Customer 3", 3);
            (it);

When using the value:
Copy the codeThe code is as follows:

int relationtype = ((KeyValueItem)).Value;