本文整理汇总了C#中System.Collections.ObjectModel.Collection<T>.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Collection<T>.Add方法的具体用法?C# Collection<T>.Add怎么用?C# Collection<T>.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.ObjectModel.Collection<T>
的用法示例。
在下文中一共展示了Collection<T>.Add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class Demo
{
public static void Main()
{
Collection<string> dinosaurs = new Collection<string>();
dinosaurs.Add("Psitticosaurus");
dinosaurs.Add("Caudipteryx");
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Muttaburrasaurus");
Console.WriteLine("{0} dinosaurs:", dinosaurs.Count);
Display(dinosaurs);
Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}",
dinosaurs.IndexOf("Muttaburrasaurus"));
Console.WriteLine("\nContains(\"Caudipteryx\"): {0}",
dinosaurs.Contains("Caudipteryx"));
Console.WriteLine("\nInsert(2, \"Nanotyrannus\")");
dinosaurs.Insert(2, "Nanotyrannus");
Display(dinosaurs);
Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]);
Console.WriteLine("\ndinosaurs[2] = \"Microraptor\"");
dinosaurs[2] = "Microraptor";
Display(dinosaurs);
Console.WriteLine("\nRemove(\"Microraptor\")");
dinosaurs.Remove("Microraptor");
Display(dinosaurs);
Console.WriteLine("\nRemoveAt(0)");
dinosaurs.RemoveAt(0);
Display(dinosaurs);
Console.WriteLine("\ndinosaurs.Clear()");
dinosaurs.Clear();
Console.WriteLine("Count: {0}", dinosaurs.Count);
}
private static void Display(Collection<string> cs)
{
Console.WriteLine();
foreach( string item in cs )
{
Console.WriteLine(item);
}
}
}
输出:
4 dinosaurs: Psitticosaurus Caudipteryx Compsognathus Muttaburrasaurus IndexOf("Muttaburrasaurus"): 3 Contains("Caudipteryx"): True Insert(2, "Nanotyrannus") Psitticosaurus Caudipteryx Nanotyrannus Compsognathus Muttaburrasaurus dinosaurs[2]: Nanotyrannus dinosaurs[2] = "Microraptor" Psitticosaurus Caudipteryx Microraptor Compsognathus Muttaburrasaurus Remove("Microraptor") Psitticosaurus Caudipteryx Compsognathus Muttaburrasaurus RemoveAt(0) Caudipteryx Compsognathus Muttaburrasaurus dinosaurs.Clear() Count: 0