Skip to content

Instantly share code, notes, and snippets.

@randomPoison
Created October 12, 2016 21:08
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 randomPoison/7b5ba10213faf817b6321d9137e0d0a7 to your computer and use it in GitHub Desktop.
Save randomPoison/7b5ba10213faf817b6321d9137e0d0a7 to your computer and use it in GitHub Desktop.
C# is the worst and I hate it
public void NotACoroutine()
{
Action callback = null;
for (var index = 0; index < 5; index++)
{
Guid guid = Guid.NewGuid();
var indexCopy = index;
Debug.Log("Guid in loop body: " + guid + ", index: " + index + ", index copy: " + indexCopy);
if (callback == null)
{
Debug.Log("Callback hasn't been set, setting it now");
callback = () => {
Debug.Log("guid: " + guid.ToString() + ", index: " + index + ", index copy: " + indexCopy);
};
}
}
callback();
}
// Prints:
//
// Guid in loop body: e75cb8c1-f068-46d4-9159-19a3377a2e0b, index: 0, index copy: 0
// Callback hasn't been set, setting it now
// Guid in loop body: 3f0ce292-d590-4c27-bde3-125f63ab518d, index: 1, index copy: 1
// Guid in loop body: ab510c89-a609-40c0-97a7-18ac684fb769, index: 2, index copy: 2
// Guid in loop body: 66b5ede1-85b9-4d71-9719-67d87864c52b, index: 3, index copy: 3
// Guid in loop body: 7fa371bd-e4fd-4a21-a214-ab77c9fa9076, index: 4, index copy: 4
// guid: e75cb8c1-f068-46d4-9159-19a3377a2e0b, index: 5, index copy: 0
private IEnumerator CasinoGamesSuccess(List<SlotInfo> games)
{
yield return null;
Action callback = null;
for (var index = 0; index < 5; index++)
{
Guid guid = Guid.NewGuid();
var indexCopy = index;
Debug.Log("Guid in loop body: " + guid + ", index: " + index + ", index copy: " + indexCopy);
if (callback == null)
{
Debug.Log("Callback hasn't been set, setting it now");
callback = () => {
Debug.Log("guid: " + guid.ToString() + ", index: " + index + ", index copy: " + indexCopy);
};
}
}
callback();
}
// Prints:
//
// Guid in loop body: e75cb8c1-f068-46d4-9159-19a3377a2e0b, index: 0, index copy: 0
// Callback hasn't been set, setting it now
// Guid in loop body: 3f0ce292-d590-4c27-bde3-125f63ab518d, index: 1, index copy: 1
// Guid in loop body: ab510c89-a609-40c0-97a7-18ac684fb769, index: 2, index copy: 2
// Guid in loop body: 66b5ede1-85b9-4d71-9719-67d87864c52b, index: 3, index copy: 3
// Guid in loop body: 7fa371bd-e4fd-4a21-a214-ab77c9fa9076, index: 4, index copy: 4
// guid: 7fa371bd-e4fd-4a21-a214-ab77c9fa9076, index: 5, index copy: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment