使用Java Iterator,我使用该hasNext方法来确定迭代是否具有更多元素(不消耗元素) - 因此,hasNext就像一个" Peek"方法.
我的问题:C#的通用s 有什么类似于" hasNext"或" Peek"的方法IEnumerator吗?
关于如何实现双向枚举器(这里,这里)的问题已被问过几次.我的问题不是如何(对于大多数情况来说这是微不足道的),但为什么 .NET平台中不存在这样的类型.
public interface IBidirectionalEnumerator<T> : IEnumerator<T>
{
bool MovePrev();
}
Run Code Online (Sandbox Code Playgroud)
显然,有许多集合类型无法实现这一点,因为它MoveNext()是破坏性的或改变底层集合的状态.但反过来,很多类型可以平凡实现这一点(List,IList,LinkedList,array).
为什么不存在这种类型?
我有一个foreach需要转换为for或while循环的循环.我的循环看起来像这样:
foreach (Item item in Items)
{
// some stuff
}
Run Code Online (Sandbox Code Playgroud)
什么是等价物for或while循环?
我想我需要用来GetEnumerator获得IEnumerator<Item>,但我不知道如何正确使用它.Items不是列表,否则我会使用索引.
我是新人,对" yield" 有点困惑.但最后我明白它是如何运作的WaitForSeconds
但我看不出" yield return 0"和" yield return null" 之间的区别.
他们都在等待下一帧执行吗?
对不起,我的英语不好.非常感谢你.
当我实现IEnumerable<T>界面时,我看到两个GetEnumerator方法:一个返回IEnumerator和另一个IEnumerator<T>.我何时会使用其中一种?
看看这段代码:
public class myWords : IEnumerable<string>
{
string[] f = "I love you".Split(new string[]{"lo"},StringSplitOptions.RemoveEmptyEntries);
public IEnumerator<string> GetEnumerator()
{
return f.Select(s => s + "2").GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return f.Select(s => s + "3").GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
跑步:
myWords m = new myWords();
foreach (var s in m)
{
Console.WriteLine(s);
}
Run Code Online (Sandbox Code Playgroud)
产量
I 2
ve you2 // notice "2", so the generic Ienumerator has executed.
Run Code Online (Sandbox Code Playgroud)
我知道非通用IEnumerator版本是为了兼容性.
题:
IEnumerator?假设你为下面的代码编写了一个自定义枚举器:
public class School : IEnumerable<Student>
Run Code Online (Sandbox Code Playgroud)
然后在客户端代码中,您执行了以下操作:
static void Main(string[] args)
{
var school = CreateSchoolWithStudents();
var query = from student in school
where student.Name.StartsWith("S")
select student;
Debugger.Break();
}
private static School CreateSchoolWithStudents()
{
return new School
{
new Student { Name = "Sathyaish" },
new Student { Name = "John" },
new Student { Name = "Carol" },
new Student { Name = "Peter" }
};
}
Run Code Online (Sandbox Code Playgroud)
然后,在类的MoveNext方法实现上设置一个断点StudentEnumerator.
然后,当您运行代码并且在构造查询/ IEnumerable之后调试器中断,并且您Results View在下面显示的图片中展开了类似内容时,Visual Studio如何评估序列而不会破坏其枚举器MoveNext …
我目前正在尝试在Unity的上下文中理解IEnumerator和Coroutine,并且对"yield return null"执行的内容不太自信.目前我认为它基本上暂停并等待下一帧,并且在下一帧中它将再次执行while语句.
如果我省略"yield return null",它似乎会立即移动到目的地或者"跳过很多帧".所以我想我的问题是在这个while循环中这个"yield return null"函数是如何实现的,为什么有必要这样做.
void Start () {
StartCoroutine(Move());
}
IEnumerator Move(){
while (a > 0.5f){
... (moves object up/down)
yield return null; // <---------
}
yield return new WaitForSeconds(0.5f);
.... (moves object up/down)
StartCoroutine(Move());
}
Run Code Online (Sandbox Code Playgroud) 下面是我想强调的差异的一个简单示例。
使用协程:
public float repeatRate = 5f;
void Start()
{
StartCoroutine("RepeatSomething");
}
IEnumerator RepeatSomething()
{
while (true)
{
yield return new WaitForSeconds(repeatRate);
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
使用Update()和Time.deltaTime:
public float repeatRate = 5f;
private float timer = 0;
void Update()
{
if (timer < 0)
{
// Do something
timer = repeatRate;
}
timer -= Time.deltaTime;
}
Run Code Online (Sandbox Code Playgroud)
我什么时候应该使用一个而不是另一个,每个的优点/缺点是什么?
我有一个实现的类IEnumerator<string>.见下文:
public class MyClass : IEnumerator<string>
{
public bool MoveNext()
{
//....
}
//Implement other required methods....
//Confusion lies below:
public string Current { get { return this.CurrentLine; } }
//Why do I need to implement IEnumerator.Current?! In my tests, it's not even called during my iteration
object IEnumerator.Current { get { throw new NotImplementedException(); } }
}
Run Code Online (Sandbox Code Playgroud)
除了IEnumerator<T>接口和IEnumerator接口(IEnumerator<T>继承)上都存在.Current属性之外,实现它的重点是什么?如上所述它甚至没有被称为.
ienumerator ×10
c# ×9
.net ×4
coroutine ×3
ienumerable ×3
yield ×2
enumerator ×1
enumerators ×1
foreach ×1
iterator ×1
linq ×1
peek ×1
time ×1