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

list.Add(1);

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);

                count++;

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

                newList.RemoveAt(index);

            }

            Console.WriteLine(count);

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)

            {

                Console.WriteLine(item);

           }

foreach (KeyValuePair<string,string> itemin dic)

            {

                Console.WriteLine(item.Key);

           }

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)

            {

                Console.WriteLine(item);

            }

Technology
©2019-2020 Toolsou All rights reserved,
Determine whether the current object is an array 4 Methods Go language Array initialization and basic operations Novices play hiss HI3520D Development board ( One , upgrade )( Essence )2020 year 7 month 12 day webpack Use of common plug-ins el-select At the same time label and value value keras Data generator -- Data enhancement Maximum security risk of cloud computing : Unclear safety responsibility The project followed for a year , The customer finally said no python in switch_to_alert Usage of ( Essence )2020 year 6 month 26 day C# Class library GUID Help class