Home >Backend Development >C#.Net Tutorial >Learn more about the differences between arrays, Lists, and ArrayLists
Some knowledge points may have been used in daily life, but in actual development we may only know them and not know why, so regular summaries will be of great help to our improvement and progress. The following article will introduce to you the differences between arrays, Lists and ArrayLists. I hope it will be helpful to you.
The difference between array, List and ArrayList
Arrays are stored continuously in memory , so its indexing speed is very fast, and assigning and modifying elements is also very simple, such as:
string[] s=new string[3]; //赋值 s[0]="a"; s[1]="b"; s[2]="c"; //修改 s[1]="b1";
But arrays also have some shortcomings. For example, it is very troublesome to insert data between two data in an array. When declaring an array, we must also specify the length of the array. If the length of the array is too long, it will cause a waste of memory. If the length of the array is too short, it will cause Data overflow error. This becomes very troublesome if we don't know the length of the array when declaring it. C# first provided the ArrayList object to overcome these shortcomings.
ArrayList is a special class provided by the .Net Framework for data storage and retrieval. It is part of the namespace System.Collections. Its size dynamically expands and contracts according to the data stored in it. Therefore, we do not need to specify its length when declaring the ArrayList object. ArrayList inherits the IList interface, so it can easily add, insert and remove data. For example:
ArrayList list = new ArrayList(); //新增数据 list.Add("abc"); list.Add(123); //修改数据 list[2] = 345; //移除数据 list.RemoveAt(0); //插入数据 list.Insert(0, "hello world");
In the list, we not only inserted the string "abc", but also inserted the number 123. In this way, inserting different types of data into ArrayList is allowed. Because ArrayList will treat all data inserted into it as object type. In this way, when we use the data in the ArrayList to deal with the problem, a type mismatch error is likely to be reported, which means that the ArrayList is not type safe. Even if we ensure that we are careful when inserting data and insert the same type of data, we still need to convert them into the corresponding original types for processing when using them. This involves boxing and unboxing operations, which will cause great performance losses.
The concept of boxing and unboxing: To put it simply: Boxing: It is to pack value type data into an instance of a reference type, such as assigning the value 123 of the int type to Object object o
int i=123; object o=(object)i;
Unboxing: It is to extract the value type from the reference data. For example, assign the value of object object o to an int type variable i
object o=123; int i=(int)o;
The process of boxing and unboxing is It's very performance-intensive.
It is precisely because ArrayList has the disadvantages of unsafe types and boxing and unboxing that the concept of generics appeared after C# 2.0. The List class is the generic equivalent of the ArrayList class. Most of its usage is similar to ArrayList, because the List class also inherits the IList interface. The most critical difference is that when declaring the List collection, we also need to declare the object type of the data in the List collection. For example:
Listbd43222e33876353aff11e13a7dc75f6 list = new Listbd43222e33876353aff11e13a7dc75f6(); //新增数据 list.Add(123); //修改数据 list[0] = 345; //移除数据 list.RemoveAt(0);
In the above example, if we insert the string character "hello world" into the List collection, the IDE will report an error and fail to compile. This avoids the type safety issues and performance issues of boxing and unboxing mentioned earlier.
At the same time, List cannot be constructed, but you can create a reference to List as above, and ListArray can be constructed.
List list; //正确 list=null; List list=new List(); // 是错误的用法
List list = new ArrayList(); This sentence creates an ArrayList object and traces it back to the List. At this time it is a List object. Some ArrayList has properties and methods that List does not, so it can no longer be used. ArrayList list=new ArrayList(); creates an object and retains all the properties of ArrayList.
Benefits of List Generics:
The generics feature shifts the task of type safety from you to the compiler by allowing you to specify the specific types that a generic class or method operates on. There is no need to write code to detect whether the data type is correct because the correct data type is enforced at compile time. Reduces the need for type casts and the possibility of runtime errors. Generics provide type safety without the overhead of multiple implementations.
This article comes from the C#.Net Tutorial column, welcome to learn!
The above is the detailed content of Learn more about the differences between arrays, Lists, and ArrayLists. For more information, please follow other related articles on the PHP Chinese website!