Skip to content

Instantly share code, notes, and snippets.

@Kritner
Last active March 9, 2017 14:34
Show Gist options
  • Save Kritner/332c9d87c0a82354a880351a43d93db8 to your computer and use it in GitHub Desktop.
Save Kritner/332c9d87c0a82354a880351a43d93db8 to your computer and use it in GitHub Desktop.
Changed loop max within loop body
using System;
public class Program
{
public static void Main()
{
Thing thing = new Thing();
for (int i = 0; i < thing.GetIterations; i++)
{
thing.DoStuff();
Console.WriteLine(i);
}
// Outputs: (I'm pretty suprised this works)
/*
0
1
2
3
4
*/
}
}
public interface IThing
{
void DoStuff();
int GetIterations { get; }
}
public class Thing : IThing
{
private int number = 10;
public int GetIterations { get { return number; } }
public void DoStuff()
{
number = 5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment