2015-05-28 28 views
8

aşağıdaki arayüze sahiptir:Anlamı C# '

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable 
{ 
    T this [int index] { get; set; } 
    int IndexOf (T item); 
    void Insert (int index, T item); 
    void RemoveAt (int index); 
} 

çizgiyi

T this [int index] { get; set; } 

Ne anlama geliyor anlamıyorum?

+0

Bu, 'myList [myInteger] = foo;' veya 'T foo = myList [myInteger]' işlevini çağırırsanız, 'foo' türdeyken iç işleyiş yapar' get_Item' ve 'set_Item' için ekstra bir yöntem aldığınız anlamına gelir. 't'. – atlaste

cevap

9

. Bu, IList<T> list ve int index için list[index]'un get ve set değerini alabileceğiniz anlamına gelir.

Dokümantasyon:

public interface IReadOnlyList<out T> : IReadOnlyCollection<T>, 
    IEnumerable<T>, IEnumerable 
{ 
    int Count { get; } 
    T this[int index] { get; } 
} 

Ve bu arayüzün örnek bir uygulama:

public class Range : IReadOnlyList<int> 
{ 
    public int Start { get; private set; } 
    public int Count { get; private set; } 
    public int this[int index] 
    { 
     get 
     { 
      if (index < 0 || index >= Count) 
      { 
       throw new IndexOutOfBoundsException("index"); 
      } 
      return Start + index; 
     } 
    } 
    public Range(int start, int count) 
    { 
     this.Start = start; 
     this.Count = count; 
    } 
    public IEnumerable<int> GetEnumerator() 
    { 
     return Enumerable.Range(Start, Count); 
    } 
    ... 
} 

Şimdi böyle bir kod yazabilirsiniz:

IReadOnlyList<int> list = new Range(5, 3); 
int value = list[1]; // value = 6 
Indexers in Interfaces (C# Programming Guide)

IReadOnlyList<T> arayüzü düşünün

+0

Bu kabin yardımcı olduğunda bana senaryoyu anlatabilir misin –