Last active
October 18, 2022 09:17
-
-
Save aemloviji/968d43dddc83a4285c1783ae1ca523b2 to your computer and use it in GitHub Desktop.
It is an alternative way to loop in csharp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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