Is there a way I can make this code more compact with a lambda or an anonymous function or something like that?
static int chancesTotal = _total_chances();
static int _total_chances()
{
int x = 0;
foreach (var c in chances)
x += c.Value;
return x;
}
That is, I'd like something like
int chancesTotal = () =>
{
int x = 0;
foreach (var c in chances)
x += c.Value;
return x;
}();
But it doesn't work.
Edit: this solution proposed by @pinkfloydx33 is close to what I had asked.
using System;
using System.Collections.Generic;
List<int> chances = new List<int>();
int chancesTotal = ((Func<int>)(() =>
{
int x = 0;
foreach (var c in chances)
x += c;
return x;
}))();