Reputation: 544
[Edit] I forgot to add the static Keyword and didn't relaize this because i called the class Statics
I am creating a Blazor client-side application.
I am trying to create a static method that i want to return a Random name from a file.
public class Statics
{
private static List<string> _names;
public Statics()
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "[AssemblyName].Stuff.first-names.json";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
_names = JsonSerializer.Deserialize<List<string>>(result);
}
}
public static string GetRandomName()
{
var random = new Random();
return _names[random.Next(_names.Count)];
}
}
I am trying to load the file in a static constructor, to not load the file more than one time, and insted save the names in memory.
But when i call the GetRandomName method i get a nullrefrenceexception on the list
It works fine if i copy the code to load the file into the method
public static string GetRandomName()
{
var random = new Random();
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "[AssemblyName].Stuff.first-names.json";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
_names = JsonSerializer.Deserialize<List<string>>(result);
}
return _names[random.Next(_names.Count)];
}
I have also tried just to populate the list with any string in the constructor (this didn't work either)
public class Statics
{
private static List<string> _names;
public Statics()
{
_names = new List<string>() { "hello", "World", "Something" };
}
public static string GetRandomName()
{
var random = new Random();
return _names[random.Next(_names.Count)];
}
}
I understand the concept of a static constructor in c# as
When you reference a field or property in a static class with a static constructor. you are insured that the constructor will run before you access the field or property (but you cannot control precisely when).
Is the problem in my code? my understanding? or the blazer mono runtime?
Upvotes: 0
Views: 655
Reputation: 1503100
Your class doesn't have a static constructor. public Statics() is an instance constructor. To be a static constructor, you'd need to use the static keyword (and remove the public modifier).
So change this:
public Statics()
to
static Statics()
... and I suspect everything will work fine, although if your class is really only meant to include static members, you might want to declare it as a static class too.
I'd also at least be wary of doing too much work in a static constructor. Problems in type initializers (including static constructors) can be very hard to resolve - and currently, you've got synchronous IO in a location that's quite hard for anything using your code to change in terms of the timing.
Maybe it's okay - it's just something to be aware of.
Upvotes: 3