Generic set

List Generic set

stay C# in , Provides a List Generic set , This set not only has ArrayList Functions of collections , And it's guaranteed List Collection can only add elements of the same type , No type conversion issues

ArrayList list = new ArrayList();

Add The parameter type of the method is object Type integer when passing parameters 1 Will be converted into object type   This is a boxing operation


If implemented  list[0] + 2  Get the right results   To list[0] Convert to int type   That means unpacking  

Console.WriteLine((int)list[0] + 2);

The generic combination determines the element types in the declaration , The data type passed in can only be consistent with the type defined at the time of declaration . Generics can be compiled at , Provides powerful type checking , Reduce display conversion between data types , Packing operation and type check at run time .

   List<string> list =newList<string>();

GetRange Used to intercept subscripts from X Start of Y Element is a new collection

 List<int> newList = numList.GetRange(2, 3);

Record the number of lookup elements

            int count = 0;

            while (newList.IndexOf(2) != -1)


                index = newList.IndexOf(2);


*** Because this step needs to delete the elements in the collection , Therefore, a new collection is usually copied for operation ***




Dictionary Generic set

Hashtable The keys and values in the collection are by default object
type , This makes users inevitably encounter the problem of type conversion when taking value . To solve this problem ,C# Provided in Dictionary Generic set . The keys and values in this collection can only be of one type .

Dictionary<string,string> dic =newDictionary<string,string>();

The key is unique in a dictionary

Different keys can correspond to the same value in a dictionary

dic.Add(" Zhang Jie "," Goofy ");

dic.Add(" Liu Huan "," Hero song ");

dic.Add(" Jay Chou "," blue and white porcelain ");

dic.Add(" Liu Ying "," blue and white porcelain ");

ContainsKey Determine whether a specific key is included

  bool key =dic.ContainsKey(" Jay Chou ");

ContainsValue Determine whether to include specific elements

bool value = dic.ContainsValue(" Hero song ");          

dic.Keys Get collection of keys

KeyCollection keys = dic.Keys;

Effective use cases of objects in dictionary collection

Dictionary<string,Book> books =newDictionary<string,Book>(); books.Add(
"0000000",newBook("C# The way of programming ",56," Wang Yao "));

books.Add("0000001", new Book("C# From entry to mastery ", 98,"YY"));

books.Add("0000002", new Book("C# From getting started to giving up ", 2," Fast Asia "));

Two methods of traversing the whole dictionary set

foreach (var itemin dic)




foreach (KeyValuePair<string,string> itemin dic)




SortedDictionary Indicates that the dictionary set is sorted by key

SortedDictionary<string,string> dic =newSortedDictionary<string,string>();

HashSet Generic set

HashSet It is characterized by different elements in the set , This property is in the build 1-100 It can be effectively reflected in the random number cases of , Based on set properties ,100 Number direct random output .

HashSet<int> hs =newHashSet<int>();

  Random random = new Random();

            while (hs.Count < 100)


                hs.Add(random.Next(1, 101));



            foreach (var itemin hs)




©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple )os Simple use of module Browser kernel ( understand ) Some East 14 Pay change 16 salary , Sincerity or routine ?HashMap Explain in detail It's unexpected Python Cherry tree (turtle The gorgeous style of Library )html Writing about cherry trees , Writing about cherry trees