Hi
I am getting error - A Static readonly field cannot be assigned except in a static construcor or a variable initializer.
static async Task Main(string[] args)
{
_duration = TimeSpan.Zero;
_random = new Random();
await Start();
}
Thanks
Amit MohantyPosted Apr 8, 2024, 12:13 PM
You're trying to assign a value to a static readonly field (_duration or _random) outside of a variable initializer or a static constructor. This is not allowed because readonly fields can only be assigned a value either at the time of declaration or within a static constructor.
So you can use a static constructor to assigned values (_duration or _random) dynamically at runtime or remove the readonly modifier from the fields.
Static constructor:
Remove the readonly: