.NET Core CSharp Intermediate articles 2-2 List, ArrayList and Dictionary

.NET Core CSharp Intermediate articles 2-2

This section contains the List, ArrayList, and Dictionary

Brief introduction

In the previous article, we learned to use the array, but the array has a big problem is the lack of storage space, we have the usual solution is to define an array of absolutely adequate, which is usually great, but would have resulted in memory Loss. We always want to have a storage array according to the needs of a more dynamic change. On one of the comprehensive title has been vague leads to the concept of List. This session we will explain in detail List.

At the same time, sometimes we do not just want an array of store our data. For example, I hope there are some data:

Someone transcript follows:

  • 80 points Language
  • Math 90 points
  • English 87

For these data, we use an array is not a good feedback on this score, this time we need to use our dictionary stored.

List、ArrayList

ArrayList

As said above, the array is a contiguous storage space, access speed is very fast, but you must specify the size, this time we can use ArrayList use. ArrayList is a class located System.Collections, inheritance and IList interface, provide operational data. It is better than the array place, it does not specify any size and type, can be used directly.

ArrayList al = new ArrayList();
al.Add("test");
al.Add(1234);
//修改数据
al[1] = 4;
//移除数据
al.RemoveAt(0);
//插入数据
al.Insert(0, "qwe");

Looks very easy to use for it, you can insert and modify different data. But in fact, this is a great loss of operating performance. Because different types of data inserted in ArrayList is allowed, but at the time of subsequent data processing, the internal ArrayList as all data will be processed Object type, so when traversing in each of the data, and packing occurs unboxing operations in the last lecture we discussed, frequent loss of entitlements, is extremely performance. Therefore, ArrayList is not often used in practice.

Generic List

In order to solve the ArrayList different types of entitlements, leading to insecurity and, we use the generic List class. List ArrayList class is a generic class equivalence class, most of its uses are similar to ArrayList, because the List class also inherits IList interface. The key difference is that when you declare a List collection, we also need to declare its object type in the List collection of data, which is the generic parameters. We
integrated exercises of primary articles have been vaguely leads to the part about the List. For List, which is defined as follows:

List<T> list = new List<T>();
list.Add(new T());
list[0];
list.Remove(T);

For List, which implements a very important interface --IEnumerable, which means List support the use of foreach loop to traverse the interior elements. However, when using foreach, illegal when the following operations:

foreach(var item in MyList)
{
    MyList.Remove(item);//不过我相信没有人那么干,但是....
    //这种操作我不止一次见过有人问我
    if(item.something == something)
    {
        MyList.Remove(item);
    }

}

This time, you need to carefully back memories before we explain foreach loop, delete dynamic in this way in a foreach loop element is not legitimate, why? Because the foreach loop calls MoveNext () method, you can imagine a node becomes a node attached to a string collection, you can only access a node backwards, which means you have to know before a node can after visiting a node, assuming that when you have access to a node, you delete it, then the subsequent node access can not be accessed. There is no method to solve it? Sure, but you can only use a for loop, List has a property called Count, this represents the number of all elements in the current List have, and realize the List indexer, that is to say, can be similar to the List MyList [0] way to visit, this time, you should use a for loop dynamic delete as follows:

for(int i =0;i<MyList.Count;i++)
{
    if(MyList[i].something == something)
    {
        MyList.Remove(MyList[i]);
    }
}

Dictionary Dictionary

You must have been mentioned in the introduction of over-demand. Many times a simple index value is no way to give us more information, we always tend to use a key-value pairs to store data. So Dictionary will be a good solution to your problem. Its basic structure is modified by two generic parameter, Dictionary <TKey, TValue>, in front of the key type, followed by the type of value, you can also Dictionary understood as a special set. Its use as follows:

Dictionary<string,string> dict = new Dictionary<string,string>();
dict.Add("广东","广州");
dict.Add("江西","南昌");
dict["江西"];
dict.remove("广东");

Generally speaking, we rarely use the foreach direct access Dictionary, because the result of the iteration is a key-value pairs, General Dictionary of Value List to the majority, and therefore are generally iterative Key.

Dictionary List and most operations are close, will not elaborate too much here.

IEnumerable and IList interface

Collection (List) when these two important interfaces interface implementation, IEnumerable provides iterative function, IList provides the corresponding set of operations, we can be very good to learn from their metadata.

IEnumerable interface

It is defined in the following metadata:

    public interface IEnumerable<out T> : IEnumerable
    {
        //
        // 摘要:
        //     Returns an enumerator that iterates through the collection.
        //
        // 返回结果:
        //     An enumerator that can be used to iterate through the collection.
        IEnumerator<T> GetEnumerator();
    }

We can clearly find generic parameters have modified out keywords, that is to say, we support the IEnumerable is covariant. We can easily convert IEnumerable type of data to other data, such as:

IEnumerable<string> strs = new IEnumerable<string>();
IEnumerable<object> obj = strs;

So when I normally use, I would recommend using some of the data IEnumerable instead of List of operation.

IList interface

The old rules, take a look at the metadata

 public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
 {
     //省略
 }

Here you can find IList does not support covariant, are invariant, then the use of the following is not legal:

IList<string> strs = new IList<string>();
IList<object> obj = strs;

If my article help you, please have your github .NET Core Guide project helped me a star, a concern and recommendation in the garden midpoint blog.

Github

BiliBili Home

WarrenRyan's Blog

Blog Park

Guess you like

Origin www.cnblogs.com/WarrenRyan/p/11294784.html