Skip to content

Instantly share code, notes, and snippets.

@kolosovpetro
Created March 20, 2021 09:41
Show Gist options
  • Save kolosovpetro/fe2d63a2d0a693942a4b2354b1430305 to your computer and use it in GitHub Desktop.
Save kolosovpetro/fe2d63a2d0a693942a4b2354b1430305 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public class CustomEnumerable
{
// A custom enumerator which has a Current property and a MoveNext() method, but does NOT implement IEnumerator.
public class CustomEnumerator
{
private readonly CustomEnumerable _enumerable;
private int _index = -1;
public CustomEnumerator(CustomEnumerable enumerable)
{
_enumerable = enumerable;
}
private IList<string> Items
{
get { return _enumerable._Items; }
}
public string Current
{
get { return _index >= 0 ? Items[_index] : null; }
}
public bool MoveNext()
{
if (_index < Items.Count-1)
{
_index++;
return true;
}
return false;
}
}
private IList<string> _Items;
public CustomEnumerable(params string[] items)
{
_Items = new List<string>(items);
}
public CustomEnumerator GetEnumerator()
{
return new CustomEnumerator(this);
}
}
public class C {
public void M() {
CustomEnumerable enumerable = new CustomEnumerable("One", "Two", "Three");
// "foreach" will work on any type that has a GetEnumerator() method, which returns
// a type that has a Current property getter and a MoveNext() method.
foreach (string item in enumerable)
{
Console.WriteLine(item);
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public class CustomEnumerable
{
public class CustomEnumerator
{
private readonly CustomEnumerable _enumerable;
private int _index = -1;
private IList<string> Items
{
get
{
return _enumerable._Items;
}
}
public string Current
{
get
{
return (_index >= 0) ? Items[_index] : null;
}
}
public CustomEnumerator(CustomEnumerable enumerable)
{
_enumerable = enumerable;
}
public bool MoveNext()
{
if (_index < Items.Count - 1)
{
_index++;
return true;
}
return false;
}
}
private IList<string> _Items;
public CustomEnumerable(params string[] items)
{
_Items = new List<string>(items);
}
public CustomEnumerator GetEnumerator()
{
return new CustomEnumerator(this);
}
}
public class C
{
public void M()
{
string[] array = new string[3];
array[0] = "One";
array[1] = "Two";
array[2] = "Three";
CustomEnumerable customEnumerable = new CustomEnumerable(array);
CustomEnumerable.CustomEnumerator enumerator = customEnumerable.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
string current = enumerator.Current;
Console.WriteLine(current);
}
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
}
{
"version": 1,
"target": "C#",
"mode": "Debug"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment