目录
42.Sort()
43.ToArray()
44.ToString()
45.TrimExcess()
46.TrueForAll(Predicate)
C# List 详解一
1.Add(T),2.AddRange(IEnumerable),3.AsReadOnly(),4.BinarySearch(T),
C# List 详解二
5.Clear(),6.Contains(T),7.ConvertAll(Converter),8.CopyTo(Int32, T[], Int32, Int32),9.CopyTo(T[]),10.CopyTo(T[], Int32)
C# List 详解三
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 详解四
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 详解五
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 详解六
35.MemberwiseClone(),36.Remove(T),37.RemoveAll(Predicate),38.RemoveAt(Int32),39.RemoveRange(Int32, Int32),40.Reverse(),41.Reverse(Int32, Int32)
C# List 详解七
42.Sort(),43.ToArray(),44.ToString(),45.TrimExcess(),46.TrueForAll(Predicate)
C# List 详解一_熊思宇的博客-CSDN博客
C# List 详解二_熊思宇的博客-CSDN博客
C# List 详解三_熊思宇的博客-CSDN博客
C# List 详解四_熊思宇的博客-CSDN博客
C# List 详解五_熊思宇的博客-CSDN博客
C# List 详解六_熊思宇的博客-CSDN博客
使用默认比较器对整个 List
public void Sort ();
案例:
using System;
using System.Collections.Generic;
namespace ListTest
{
internal class Program
{
static void Main(string[] args)
{
List list = new List() { 4, 3, 45, 14, 31 };
list.Sort();
Console.WriteLine(string.Join("-", list));
Console.ReadKey();
}
}
}
运行:
将 List
public T[] ToArray ();
返回
T[]
一个包含 List
案例:
using System;
using System.Collections.Generic;
namespace ListTest
{
internal class Program
{
static void Main(string[] args)
{
List list = new List() { 4, 3, 45, 14, 31 };
int[] ints = list.ToArray();
Console.WriteLine(string.Join("-", ints));
Console.ReadKey();
}
}
}
运行:
返回表示当前对象的字符串。(继承自 Object)
public virtual string? ToString ();
返回
String
表示当前对象的字符串。
案例:
using System;
using System.Collections.Generic;
namespace ListTest
{
internal class Program
{
static void Main(string[] args)
{
List list = new List() { 4, 3, 45, 14, 31 };
string value = list[0].ToString();
Console.WriteLine(value);
Console.ReadKey();
}
}
}
运行:
将容量设置为 List
public void TrimExcess ();
案例:
using System;
using System.Collections;
using System.Collections.Generic;
namespace ListTest
{
internal class Program
{
static void Main(string[] args)
{
List list = new List() { 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();
}
}
}
运行:
确定 List
public bool TrueForAll (Predicate match);
参数
match
Predicate
用于定义检查元素时要对照条件的 Predicate
返回
Boolean
如果 List
案例:
using System;
using System.Collections.Generic;
namespace ListTest
{
internal class Program
{
static void Main(string[] args)
{
List dinosaurs = new List();
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();
}
}
}
当然,这么写也是没问题的
using System;
using System.Collections.Generic;
namespace ListTest
{
internal class Program
{
static void Main(string[] args)
{
List dinosaurs = new List();
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");
}
}
}
运行:
在上面的案例中, ToLower 是将字母转为小写,EndsWith 是指当前的字符串是否以指定字符结尾,在 dinosaurs 这个 List 中,当然不是所有字符串都以 saurus 结尾,所以返回是 false。
第 7 / 7 篇 End