I have a class to display colors in order
public class Color
{
private static string[] _colors = {"Red", "Green", "Blue"};
public static int Index { get; set; }
public static string GetNextColor()
{
var retVal = _colors[Index];
Index++;
return retVal;
}
}
I am using the above class by this following code:
[HttpGet]
public string TestColor()
{
Color.Index = 0;
return string.Format("First color: {0} Second color: {1} Third color: {2}", Color.GetNextColor(), Color.GetNextColor(), Color.GetNextColor());
}
which display this result
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
First color: Red Second color: Green Third color: Blue
</string>
Now my question is
How can I reset the Index into 0 every time a page reload without resetting it as Color.Index = 0;
Based on my knowledge a static field can only be reset by app restart, so in my application, I'm always being forced to reset the Index to 0 everytime I call this static method. I just want this field "Index" to reset to 0 every time the page reload. I'm not sure if setting Color.Index = 0
for every top of my code can be avoided.