GetEnumeratorDöngüler için uzantı desteği foreach .Extension GetEnumerator support for foreach loops.

ÖzetSummary

Foreach döngüsünün, başka bir şekilde foreach modelini karşılayan bir genişletme yöntemi GetEnumerator metodunu tanımasını ve aksi durumda bir hata olması durumunda ifadenin üzerinde döngüye girmesine izin verin.Allow foreach loops to recognize an extension method GetEnumerator method that otherwise satisfies the foreach pattern, and loop over the expression when it would otherwise be an error.

MotivasyonMotivation

Bu, zaman uyumsuz ve model tabanlı ayrıştırma dahil olmak üzere C# ' deki diğer özelliklerin nasıl uygulandığı ile foreach satır içine getirir.This will bring foreach inline with how other features in C# are implemented, including async and pattern-based deconstruction.

Ayrıntılı tasarımDetailed design

Belirtim değişikliği nispeten basittir.The spec change is relatively straightforward. The foreach statementBölümü şu metinle değiştirdik:We modify The foreach statement section to this text:

Foreach deyiminin derleme zamanı işleme, ifadenin koleksiyon türü _, _Numaralandırıcı türü*_ ve _ öğe türü * öğesini belirler.The compile-time processing of a foreach statement first determines the collection type _, _enumerator type_ and _ element type of the expression. Bu belirleme işlemi aşağıdaki gibi gerçekleştirilir:This determination proceeds as follows:

  • X İfade türü bir dizi türü ise, arabirime bir örtük başvuru dönüştürmesi vardır X IEnumerable ( System.Array Bu arabirimden bu yana).If the type X of expression is an array type then there is an implicit reference conversion from X to the IEnumerable interface (since System.Array implements this interface). Koleksiyon türü _ IEnumerable arabirim, _Numaralandırıcı türü_ IEnumerator arayüztür ve _ öğesi türü dizi türünün öğe türüdür X .The collection type _ is the IEnumerable interface, the _enumerator type_ is the IEnumerator interface and the _ element type is the element type of the array type X.

  • X İfadenin türü dynamic daha sonra deyimden IEnumerable arabirime (örtük dinamik dönüştürmeler) örtük bir dönüşüm varsa.If the type X of expression is dynamic then there is an implicit conversion from expression to the IEnumerable interface (Implicit dynamic conversions). Koleksiyon türü _, IEnumerable arabirimdir ve _Numaralandırıcı türüdür*_ IEnumerator .The collection type _ is the IEnumerable interface and the _enumerator type*_ is the IEnumerator interface. varTanımlayıcı _local_variable_type * olarak verilirse, öğe türü ise, dynamic Aksi durumda object .If the var identifier is given as the _local_variable_type* then the element type is dynamic, otherwise it is object.

  • Aksi takdirde, türün X uygun bir yönteme sahip olup olmadığını saptayın GetEnumerator :Otherwise, determine whether the type X has an appropriate GetEnumerator method:

    • XTanımlayıcı GetEnumerator ve tür bağımsız değişkeni olmayan tür üzerinde üye araması gerçekleştirin.Perform member lookup on the type X with identifier GetEnumerator and no type arguments. Üye arama bir eşleşme oluşturmuyorsa veya bir belirsizlik üretir ya da bir yöntem grubu olmayan bir eşleşme üretirse, aşağıda açıklandığı gibi sıralanabilir bir arabirim olup olmadığını kontrol edin.If the member lookup does not produce a match, or it produces an ambiguity, or produces a match that is not a method group, check for an enumerable interface as described below. Üye arama yöntemi bir yöntem grubu veya eşleşme dışında bir şey üretirse bir uyarı verilmesi önerilir.It is recommended that a warning be issued if member lookup produces anything except a method group or no match.
    • Elde edilen yöntem grubunu ve boş bir bağımsız değişken listesini kullanarak aşırı yükleme çözümlemesi gerçekleştirin.Perform overload resolution using the resulting method group and an empty argument list. Aşırı yükleme çözümlemesi uygulanabilir bir yöntem olmadan sonuçlanırsa, belirsizliğe neden olur veya tek bir en iyi yöntemle sonuçlanır, ancak bu yöntem statik veya genel değil, aşağıda açıklandığı gibi sıralanabilir bir arabirim olup olmadığını kontrol edin.If overload resolution results in no applicable methods, results in an ambiguity, or results in a single best method but that method is either static or not public, check for an enumerable interface as described below. Aşırı yükleme çözümlemesi, belirsiz bir ortak örnek yöntemi veya uygulanabilir Yöntemler dışında bir şey üretirse bir uyarı verilmesi önerilir.It is recommended that a warning be issued if overload resolution produces anything except an unambiguous public instance method or no applicable methods.
    • Yöntemin dönüş türü E GetEnumerator bir sınıf, yapı veya arabirim türü değilse, bir hata oluşturulur ve başka bir adım alınmaz.If the return type E of the GetEnumerator method is not a class, struct or interface type, an error is produced and no further steps are taken.
    • Üye arama, E tanımlayıcısı Current ve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed on E with the identifier Current and no type arguments. Üye arama hiçbir eşleşme oluşturmazsa, sonuç bir hatadır veya sonuç, okumaya izin veren bir ortak örnek özelliği dışında bir hata oluşturulur ve başka hiçbir adım alınmaz.If the member lookup produces no match, the result is an error, or the result is anything except a public instance property that permits reading, an error is produced and no further steps are taken.
    • Üye arama, E tanımlayıcısı MoveNext ve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed on E with the identifier MoveNext and no type arguments. Üye arama hiçbir eşleşme oluşturmazsa, sonuç bir hatadır veya sonuç bir yöntem grubu dışında bir hata üretiyorsa, başka bir adım alınmaz.If the member lookup produces no match, the result is an error, or the result is anything except a method group, an error is produced and no further steps are taken.
    • Aşırı yükleme çözümlemesi, metot grubunda boş bir bağımsız değişken listesiyle gerçekleştirilir.Overload resolution is performed on the method group with an empty argument list. Aşırı yükleme çözümlemesi uygulanabilir bir yöntem olmadan sonuçlanırsa, bir belirsizliğe neden olur ya da tek bir en iyi yöntem ile sonuçlanır, ancak bu yöntem statik veya genel değildir veya dönüş türü değildir bool , bir hata üretilir ve başka bir adım alınmaz.If overload resolution results in no applicable methods, results in an ambiguity, or results in a single best method but that method is either static or not public, or its return type is not bool, an error is produced and no further steps are taken.
    • Koleksiyon türü _, X _Numaralandırıcı türü*_ E ve _ *öğe türü**, Current özelliğin türüdür.The collection type _ is X, the _enumerator type_ is E, and the _ element type is the type of the Current property.
  • Aksi takdirde, sıralanabilir bir arabirim olup olmadığını kontrol edin:Otherwise, check for an enumerable interface:

    • Ti' Den örtük bir dönüştürme olan tüm türler arasında X IEnumerable<Ti> , olmayan bir benzersiz tür vardır T ve ' T dynamic den ' a örtük bir dönüştürme söz konusu olduğunda, Ti IEnumerable<T> IEnumerable<Ti> koleksiyon türü _, IEnumerable<T> _Numaralandırıcı türü_ arayüztür IEnumerator<T> ve _ öğe türü olur T .If among all the types Ti for which there is an implicit conversion from X to IEnumerable<Ti>, there is a unique type T such that T is not dynamic and for all the other Ti there is an implicit conversion from IEnumerable<T> to IEnumerable<Ti>, then the collection type _ is the interface IEnumerable<T>, the _enumerator type_ is the interface IEnumerator<T>, and the _ element type is T.
    • Aksi takdirde, böyle birden çok tür varsa T , bir hata oluşturulur ve başka bir adım alınmaz.Otherwise, if there is more than one such type T, then an error is produced and no further steps are taken.
    • Aksi halde, arabirimine örtük bir dönüşüm varsa X System.Collections.IEnumerable , koleksiyon türü _ Bu arabirimdir, _Numaralandırıcı türü arayüztür_ System.Collections.IEnumerator ve _ öğe türü olur object .Otherwise, if there is an implicit conversion from X to the System.Collections.IEnumerable interface, then the collection type _ is this interface, the _enumerator type_ is the interface System.Collections.IEnumerator, and the _ element type is object.
  • Aksi takdirde, ' X ' türünün uygun bir genişletme yöntemine sahip olup olmadığını saptayın GetEnumerator :Otherwise, determine whether the type 'X' has an appropriate GetEnumerator extension method:

    • Tanımlayıcı ile tür üzerinde genişletme yöntemi arama gerçekleştirin X GetEnumerator .Perform extension method lookup on the type X with identifier GetEnumerator. Üye arama bir eşleşme oluşturmuyorsa veya bir belirsizlik üretir veya bir yöntem grubu olmayan bir eşleşme üretirse, bir hata oluşturulur ve başka bir adım alınmaz.If the member lookup does not produce a match, or it produces an ambiguity, or produces a match which is not a method group, an error is produced and no further steps are taken. Üye arama yöntemi bir yöntem grubu veya eşleşme dışında bir şey üretirse bir uyarı olması önerilir.It is recommended that a warning be issues if member lookup produces anything except a method group or no match.
    • Elde edilen yöntem grubunu ve türünde tek bir bağımsız değişkeni kullanarak aşırı yükleme çözümlemesi gerçekleştirin X .Perform overload resolution using the resulting method group and a single argument of type X. Aşırı yükleme çözümlemesi uygulanabilir bir yöntem oluşturmazsa, belirsizliğe neden olur ya da tek bir en iyi yöntemle sonuçlanır, ancak bu yöntem erişilebilir değilse, başka bir adım uygulanmaz bir hata oluşturulur.If overload resolution produces no applicable methods, results in an ambiguity, or results in a single best method but that method is not accessible, an error is produced an no further steps are taken.
      • Bu çözüm, X bir yapı türünde ise ve başvuru türü ise, ilk bağımsız değişkenin ref tarafından geçirilmesine izin verir in .This resolution permits the first argument to be passed by ref if X is a struct type, and the ref kind is in.
    • Yöntemin dönüş türü E GetEnumerator bir sınıf, yapı veya arabirim türü değilse, bir hata oluşturulur ve başka bir adım alınmaz.If the return type E of the GetEnumerator method is not a class, struct or interface type, an error is produced and no further steps are taken.
    • Üye arama, E tanımlayıcısı Current ve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed on E with the identifier Current and no type arguments. Üye arama hiçbir eşleşme oluşturmazsa, sonuç bir hatadır veya sonuç, okumaya izin veren bir ortak örnek özelliği dışında bir hata oluşturulur ve başka hiçbir adım alınmaz.If the member lookup produces no match, the result is an error, or the result is anything except a public instance property that permits reading, an error is produced and no further steps are taken.
    • Üye arama, E tanımlayıcısı MoveNext ve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed on E with the identifier MoveNext and no type arguments. Üye arama hiçbir eşleşme oluşturmazsa, sonuç bir hatadır veya sonuç bir yöntem grubu dışında bir hata üretiyorsa, başka bir adım alınmaz.If the member lookup produces no match, the result is an error, or the result is anything except a method group, an error is produced and no further steps are taken.
    • Aşırı yükleme çözümlemesi, metot grubunda boş bir bağımsız değişken listesiyle gerçekleştirilir.Overload resolution is performed on the method group with an empty argument list. Aşırı yükleme çözümlemesi uygulanabilir bir yöntem olmadan sonuçlanırsa, bir belirsizliğe neden olur ya da tek bir en iyi yöntem ile sonuçlanır, ancak bu yöntem statik veya genel değildir veya dönüş türü değildir bool , bir hata üretilir ve başka bir adım alınmaz.If overload resolution results in no applicable methods, results in an ambiguity, or results in a single best method but that method is either static or not public, or its return type is not bool, an error is produced and no further steps are taken.
    • Koleksiyon türü _, X _Numaralandırıcı türü*_ E ve _ *öğe türü**, Current özelliğin türüdür.The collection type _ is X, the _enumerator type_ is E, and the _ element type is the type of the Current property.
  • Aksi halde, bir hata oluşturulur ve başka bir adım alınmaz.Otherwise, an error is produced and no further steps are taken.

İçin await foreach kurallar benzer şekilde değiştirilmiştir.For await foreach, the rules are similarly modified. Bu spec için gerekli tek değişiklik, Extension methods do not contribute. Bu spec 'in geri kalanı, model yöntemlerinin farklı adlarıyla birlikte Yukarıdaki kurallara dayandıktan sonra, açıklamadan satırı kaldırmalıdır.The only change that is required to that spec is removing the Extension methods do not contribute. line from the description, as the rest of that spec is based on the above rules with different names substituted for the pattern methods.

BulunmaktadırDrawbacks

Her değişiklik dile ek karmaşıklık ekler ve bu potansiyel olarak, benzer şekilde tasarlanmayan öğelerin, gibi bir şekilde kullanıma almasına izin verir foreach foreach Range .Every change adds additional complexity to the language, and this potentially allows things that weren't designed to be foreached to be foreached, like Range.

AlternatiflerAlternatives

Hiçbir şey yapma.Doing nothing.

Çözümlenmemiş sorularUnresolved questions

Bu noktada yok.None at this point.