1024programmer Asp.Net Unit 5 Generic Collections

Unit 5 Generic Collections

Unit 5 Generic Collection

1. Why choose to use generic collections

Existing problems

ArrayList arrylist = new ArrayList() { 14, "hello", 29.7, true  };
 arrylist.Add("world");// object
 double dsum = 0  ;
 foreach(var item in arrylist)
 {
     dsum += Convert.ToDouble(item); //Exception occurred
 }

1. Packing and unboxing is required to access data 2. There are hidden dangers in data type conversion

Performance comparison

Non-generic collection performance

[Test]
 public void  span> Test1()
 {
     Stopwatch watch = new  Stopwatch();
     watch.Start();
     ArrayList arrayList = new  ();
     for (int i = 0; i < 2000000; i++)
     {
         arrayList.Add(i); // Packing
  }
 ​
     long sum = 0  ;
     foreach (var item in arrayList)
     {
         sum += Convert.ToInt64(item);
     }
     watch.Stop();
     Console.WriteLine("Non-generic collection time (ms):"+watch.ElapsedMilliseconds);
 }

Output result: non-generic collection time (ms): 258

Generic collection performance

[Test]
 public void  span> Test1()
 {
     Stopwatch watch = new  Stopwatch();
     watch.Start();
     var arrayList = new   List<int>  ();
     for (int i = 0; i < 2000000; i++)
     {
         arrayList.Add(i);
     }

     ]);
 }
 catch (KeyNotFoundException)
 {
     Console.WriteLine("Key = \"tif\" is not found.");
 }

 // tryValue try  Value
 string value = "";
 if (openWith.TryGetValue("tif  ", out value  ))
 {
     Console.WriteLine("For key = \"tif\", value = {0}.", value);
 }
 else
 {
     Console.WriteLine("Key = \"tif\" is not found.");
 }

 // Determine whether  Contains key
 if (!openWith.ContainsKey("  ht"  span>))
 {
     openWith.Add("ht",   "hypertrm.exe  ");
     Console.WriteLine("Value added for key = \"ht\": {0}",
         openWith["ht  "]);
 }

 // Traversal loop  , the elements are retrieved as KeyValuePair objects
 Console.WriteLine();
 foreach( KeyValuePair<  string, string> kvp in openWith )
 {
     Console.WriteLine("Key = {0}, Value = {1}",
         kvp.Key, kvp.Value);
 }

 // Get all  value set
 Dictionary<string, string>.ValueCollection valueColl =
     openWith.Values;

 // Traverse values  Collection
 Console.WriteLine();
 foreach( string s in valueColl )
 {
     Console.WriteLine("Value = {0}", s);
 }

 // Get all  set of keys
 Dictionary<string, string>.KeyCollection keyColl =
     openWith.Keys;

 // Traverse keys  Collection
 Console.WriteLine();
 foreach( string s in keyColl )
 {
     Console.WriteLine("Key = {0}", s);
 }

 // Remove  Key-value pair
 Console.WriteLine("\nRemove(  \"doc\")");
 openWith.Remove("doc")  ;

 if (!openWith.ContainsKey("doc"))
 {
     Console.WriteLine("Key \"doc\" is not found.");
 }

It is required to encapsulate the extension method ToDictionary() of OrderStateEnum and get a Dictionary, the effect is as follows

Dictionary<int,string  > dict = OrderStateEnum.ToDictionary();

Supporting video link: C#/.Net Advanced – NetEase Cloud Classroom (163.com)

The sea is broad and the fish are leaping, the sky is high and I can do whatever I want. Give me a piece of blue sky and let me fly freely.)”>Traverse key collection
Console.WriteLine();
foreach( string s in keyColl )
{
Console.WriteLine(
Key = {0}, s);
}

// Remove Key-value pair
Console.WriteLine(\nRemove( \”doc\”));
openWith.Remove(
doc) ;

if (!openWith.ContainsKey(doc))
{
Console.WriteLine(
Key \”doc\” is not found.);
}

It is required to encapsulate the extension method ToDictionary() of OrderStateEnum and get a Dictionary, the effect is as follows

Dictionary<int,string  > dict = OrderStateEnum.ToDictionary();

Supporting video link: C#/.Net Advanced – NetEase Cloud Classroom (163.com)

The sea is broad and the fish are leaping, the sky is high and I can do whatever I want. Give me a piece of blue sky and let me fly freely.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/808600

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索