本文整理匯總了C#中System.Collections.ObjectModel.Collection<T>.Item屬性的典型用法代碼示例。如果您正苦於以下問題:C# Collection<T>.Item屬性的具體用法?C# Collection<T>.Item怎麽用?C# Collection<T>.Item使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Collections.ObjectModel.Collection<T>
的用法示例。
在下文中一共展示了Collection<T>.Item屬性的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