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ırXIEnumerable(System.ArrayBu arabirimden bu yana).If the typeXof expression is an array type then there is an implicit reference conversion fromXto theIEnumerableinterface (sinceSystem.Arrayimplements this interface). Koleksiyon türü _IEnumerablearabirim, _Numaralandırıcı türü_IEnumeratorarayüztür ve _ öğesi türü dizi türünün öğe türüdürX.The collection type _ is theIEnumerableinterface, the _enumerator type_ is theIEnumeratorinterface and the _ element type is the element type of the array typeX.
Xİfadenin türüdynamicdaha sonra deyimdenIEnumerablearabirime (örtük dinamik dönüştürmeler) örtük bir dönüşüm varsa.If the typeXof expression isdynamicthen there is an implicit conversion from expression to theIEnumerableinterface (Implicit dynamic conversions). Koleksiyon türü _,IEnumerablearabirimdir ve _Numaralandırıcı türüdür*_IEnumerator.The collection type _ is theIEnumerableinterface and the _enumerator type*_ is theIEnumeratorinterface.varTanımlayıcı _local_variable_type * olarak verilirse, öğe türü ise,dynamicAksi durumdaobject.If thevaridentifier is given as the _local_variable_type* then the element type isdynamic, otherwise it isobject.Aksi takdirde, türün
Xuygun bir yönteme sahip olup olmadığını saptayınGetEnumerator:Otherwise, determine whether the typeXhas an appropriateGetEnumeratormethod:
XTanımlayıcıGetEnumeratorve tür bağımsız değişkeni olmayan tür üzerinde üye araması gerçekleştirin.Perform member lookup on the typeXwith identifierGetEnumeratorand 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ü
EGetEnumeratorbir 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 typeEof theGetEnumeratormethod is not a class, struct or interface type, an error is produced and no further steps are taken.- Üye arama,
EtanımlayıcısıCurrentve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed onEwith the identifierCurrentand 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,
EtanımlayıcısıMoveNextve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed onEwith the identifierMoveNextand 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 notbool, an error is produced and no further steps are taken.- Koleksiyon türü _,
X_Numaralandırıcı türü*_Eve _ *öğe türü**,Currentözelliğin türüdür.The collection type _ isX, the _enumerator type_ isE, and the _ element type is the type of theCurrentproperty.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ındaXIEnumerable<Ti>, olmayan bir benzersiz tür vardırTve 'Tdynamicden ' a örtük bir dönüştürme söz konusu olduğunda,TiIEnumerable<T>IEnumerable<Ti>koleksiyon türü _,IEnumerable<T>_Numaralandırıcı türü_ arayüztürIEnumerator<T>ve _ öğe türü olurT.If among all the typesTifor which there is an implicit conversion fromXtoIEnumerable<Ti>, there is a unique typeTsuch thatTis notdynamicand for all the otherTithere is an implicit conversion fromIEnumerable<T>toIEnumerable<Ti>, then the collection type _ is the interfaceIEnumerable<T>, the _enumerator type_ is the interfaceIEnumerator<T>, and the _ element type isT.- 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 typeT, then an error is produced and no further steps are taken.- Aksi halde, arabirimine örtük bir dönüşüm varsa
XSystem.Collections.IEnumerable, koleksiyon türü _ Bu arabirimdir, _Numaralandırıcı türü arayüztür_System.Collections.IEnumeratorve _ öğe türü olurobject.Otherwise, if there is an implicit conversion fromXto theSystem.Collections.IEnumerableinterface, then the collection type _ is this interface, the _enumerator type_ is the interfaceSystem.Collections.IEnumerator, and the _ element type isobject.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 appropriateGetEnumeratorextension method:
- Tanımlayıcı ile tür üzerinde genişletme yöntemi arama gerçekleştirin
XGetEnumerator.Perform extension method lookup on the typeXwith identifierGetEnumerator. Ü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 typeX. 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,
Xbir yapı türünde ise ve başvuru türü ise, ilk bağımsız değişkenin ref tarafından geçirilmesine izin veririn.This resolution permits the first argument to be passed by ref ifXis a struct type, and the ref kind isin.- Yöntemin dönüş türü
EGetEnumeratorbir 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 typeEof theGetEnumeratormethod is not a class, struct or interface type, an error is produced and no further steps are taken.- Üye arama,
EtanımlayıcısıCurrentve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed onEwith the identifierCurrentand 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,
EtanımlayıcısıMoveNextve tür bağımsız değişkenleri olmadan üzerinde gerçekleştirilir.Member lookup is performed onEwith the identifierMoveNextand 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 notbool, an error is produced and no further steps are taken.- Koleksiyon türü _,
X_Numaralandırıcı türü*_Eve _ *öğe türü**,Currentözelliğin türüdür.The collection type _ isX, the _enumerator type_ isE, and the _ element type is the type of theCurrentproperty.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.