Skip to content

Instantly share code, notes, and snippets.

@aemloviji
Last active October 18, 2022 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aemloviji/968d43dddc83a4285c1783ae1ca523b2 to your computer and use it in GitHub Desktop.
Save aemloviji/968d43dddc83a4285c1783ae1ca523b2 to your computer and use it in GitHub Desktop.
It is an alternative way to loop in csharp
using static System.Console;
WriteLine("bu csharp-di?");
foreach (var i in 1..10)
{
WriteLine($"{i} beli!!!");
}
internal static class RangeExtensions
{
public static RangeEnumerator GetEnumerator(this Range r) => new(r);
internal struct RangeEnumerator
{
private readonly int _end;
public int Current { get; private set; }
public bool MoveNext()
{
Current++;
return Current <= _end;
}
public RangeEnumerator(Range r)
{
Current = r.Start.Value - 1;
_end = r.End.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment