class Person { PRivate string firstname; public string FirstName { get {return firstname;} set {firstname = value;} } } 属性声明可以如下编码: Person p = new Person(); p.FirstName = "Lamont"; Console.WriteLine (p.FirstName);
class SampleCollection<T> { private T[] arr = new T[100]; public T this[int i] { get { return arr[i]; } set { arr[i] = value; } } }
// This class shows how client code uses the indexer class Program { static void Main(string[] args) { SampleCollection<string> stringCollection = new SampleCollection<string>(); stringCollection[0] = "Hello, World"; System.Console.WriteLine(stringCollection[0]); } }