简体   繁体   English

带有泛型的C#语法

[英]C# syntax with generics

I have a question about following variable declaration. 我有一个关于跟随变量声明的问题。 What does that mean? 这意味着什么?

List<string>.Enumerator enumerator

List is a generic type, where string serves as a type parameter. List是泛型类型,其中string用作类型参数。 How to interpret the .Enumerator after that? 之后怎么解释.Enumerator?

List<T> has a nested class in it, called Enumerator . List<T>有一个嵌套类,名为Enumerator

So the type definition of that is List<T>.Enumerator (and in your case T is a string ). 所以它的类型定义是List<T>.Enumerator (在你的情况下T是一个string )。

PS PS

Actually, List<T>.Enumerator is a struct , not a class , anyway the type definition would be the same. 实际上, List<T>.Enumerator是一个struct ,而不是一个class ,无论如何类型定义都是相同的。

In fact for all nested types it's always OuterType.NestedType 事实上,对于所有嵌套类型,它始终是OuterType.NestedType

Try this link, has all information about List.Enumerator: Enumerator at MSDN documentation 试试这个链接, 在MSDN文档中包含有关List.Enumerator: Enumerator的所有信息

In essens the enumerator is used while looping over the list with for each: "Initially, the enumerator is positioned before the first element in the collection. At this position, Current is undefined. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current." 在本质上,枚举器在循环遍历列表时使用:“最初,枚举器位于集合中的第一个元素之前。在此位置,当前未定义。因此,您必须调用MoveNext以将枚举数推进到在读取Current的值之前,集合的第一个元素。“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM