C# List Detailed Explanation VII

Table of contents

42.Sort()    

43.ToArray()    

44.ToString()    

45.TrimExcess()    

46.TrueForAll(Predicate)    


C# List Detailed Explanation 1

1.Add(T),2.AddRange(IEnumerable),3.AsReadOnly(),4.BinarySearch(T),

C# List Detailed Explanation 2

5.Clear(),6.Contains(T),7.ConvertAll(Converter),8.CopyTo(Int32, T[], Int32, Int32),9.CopyTo(T[]),10.CopyTo(T[], Int32)

C# List Detailed Explanation 3

11.Equals(Object),12.Exists(Predicate),13.Find(Predicate),14.FindAll(Predicate),15.FindIndex(Int32, Int32, Predicate),16.FindIndex(Int32, Predicate),17.FindIndex(Predicate)  

C# List Detailed Explanation 4

18.FindLast(Predicate),19.FindLastIndex(Int32, Int32, Predicate),20.FindLastIndex(Int32, Predicate),21.FindLastIndex(Predicate),22.ForEach(Action),23.GetEnumerator(),24.GetHashCode(),25.GetRange(Int32, Int32) 

C# List Detailed Explanation 5

26.GetType(),27.IndexOf(T),28.IndexOf(T, Int32),29.IndexOf(T, Int32, Int32),30.Insert(Int32, T),31.InsertRange(Int32, IEnumerable),32.LastIndexOf(T),33.LastIndexOf(T, Int32),34.LastIndexOf(T, Int32, Int32)    

C# List Detailed Explanation 6

35.MemberwiseClone(),36.Remove(T),37.RemoveAll(Predicate),38.RemoveAt(Int32),39.RemoveRange(Int32, Int32),40.Reverse(),41.Reverse(Int32, Int32)    

C# List Detailed Explanation VII

42.Sort(),43.ToArray(),44.ToString(),45.TrimExcess(),46.TrueForAll(Predicate) 

C# List Detailed Explanation 1 - Xiong Siyu's Blog - CSDN Blog

C# List Detailed Explanation II - Xiong Siyu's Blog - CSDN Blog

C# List Detailed Explanation 3 - Xiong Siyu's Blog - CSDN Blog

C# List Detailed Explanation 4_Xiong Siyu's Blog - CSDN Blog

C# List Detailed Explanation Five_Xiong Siyu's Blog - CSDN Blog

C# List Detailed Explanation 6_Xiong Siyu's Blog - CSDN Blog

42.Sort()    

Sorts the elements in the entire List<T> using the default comparator.

public void Sort ();

case:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 4, 3, 45, 14, 31 };
            list.Sort();
            Console.WriteLine(string.Join("-", list));

            Console.ReadKey();
        }
    }
}

run:

43.ToArray()    

Copies the elements of the List<T> into a new array.

public T[] ToArray ();

Returns
T[]
an array containing copies of the elements of the List<T>.

case:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 4, 3, 45, 14, 31 };
            int[] ints = list.ToArray();
            Console.WriteLine(string.Join("-", ints));

            Console.ReadKey();
        }
    }
}

run:

44.ToString()    

Returns a string representing the current object. (inherited from Object)

public virtual string? ToString ();

Returns
a String
representing the string of the current object.

case:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 4, 3, 45, 14, 31 };
            string value = list[0].ToString();
            Console.WriteLine(value);
  
            Console.ReadKey();
        }
    }
}

run:

45.TrimExcess()    

Sets the capacity to the actual number of elements in the List<T> if that number is less than a certain threshold.

public void TrimExcess ();

case:

using System;
using System.Collections;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 4, 3, 45, 14, 31 };
            Console.WriteLine("Capacity:{0}", list.Capacity);
            Console.WriteLine("Count:{0}", list.Count);
            Console.WriteLine("===================");

            list.TrimExcess();
            Console.WriteLine("Capacity:{0}", list.Capacity);
            Console.WriteLine("Count:{0}", list.Count);

            Console.ReadKey();
        }
    }
}

run:

46.TrueForAll(Predicate<T>)    

Determines whether each element in the List<T> matches the conditions defined by the specified predicate.

public bool TrueForAll (Predicate<T> match);

The parameter
match
Predicate<T>
is used to define the Predicate<T> delegate against which the condition is checked when checking the element.

Returns
a Boolean
that is true if each element in the List<T> matches the conditions defined by the specified predicate; otherwise, false. The return value is true if the list has no elements.

case:

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> dinosaurs = new List<string>();
            dinosaurs.Add("Compsognathus");
            dinosaurs.Add("Amargasaurus");
            dinosaurs.Add("Oviraptor");
            dinosaurs.Add("Velociraptor");
            dinosaurs.Add("Deinonychus");
            dinosaurs.Add("Dilophosaurus");
            dinosaurs.Add("Gallimimus");
            dinosaurs.Add("Triceratops");
            bool result = dinosaurs.TrueForAll(x => x.ToLower().EndsWith("saurus"));
            Console.WriteLine("结果:{0}", result);

            Console.ReadKey();
        }
    }
}

Of course, it's okay to write like this

using System;
using System.Collections.Generic;

namespace ListTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> dinosaurs = new List<string>();
            dinosaurs.Add("Compsognathus");
            dinosaurs.Add("Amargasaurus");
            dinosaurs.Add("Oviraptor");
            dinosaurs.Add("Velociraptor");
            dinosaurs.Add("Deinonychus");
            dinosaurs.Add("Dilophosaurus");
            dinosaurs.Add("Gallimimus");
            dinosaurs.Add("Triceratops");
            bool result = dinosaurs.TrueForAll(EndsWithSaurus);
            Console.WriteLine("结果:{0}", result);

            Console.ReadKey();
        }

        private static bool EndsWithSaurus(String s)
        {
            return s.ToLower().EndsWith("saurus");
        }
    }
}

run:

In the above case, ToLower is to convert letters to lowercase, EndsWith refers to whether the current string ends with the specified character, in the dinosaurs list, of course not all strings end with saurus, so the return is false.

Chapter 7 / 7 End

Guess you like

Origin blog.csdn.net/qq_38693757/article/details/131723842