[Unity practical skills] Several commonly used functions of the List<T> class

List<T> in C# itself provides many interfaces that can make your code very clean. I also saw a lot of code written by other programmers in the project, learn and record it~

For details, of course, go to the official help document: https://msdn.microsoft.com/zh-cn/library/6sh2ey19(v=vs.110).aspx


The simplest add, clear, and contain will not be said, but a few useful ones:


  1. Sort Sort().

    I have said this before, you can look here: http://blog.51cto.com/13638120/2084838

  2. Reverse order Reverse().

        This is to reverse the order of the list, generally used with Sort

3.ToArray()

    Convert to an array, commonly used

4.Select()

    This is to get the data in the original list and project it into the new list. It can be to get a parameter in the original list. The T of the two lists does not have to be the same, for example

Class A
{
	float b;
}

List<A> list = new List<A>();
List<B> newList = list.Select(a => a.b);

4.Max()

 Returns the largest value in the list, if T is a number

5.Average()

Returns the average of the data in the list, if T is a number.



...

Welcome to add

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324481323&siteId=291194637