Hi the intent of the code below is to return a static class which holds a related set of data. Can you recommend how I can do this better?
public class People
{
//want info to be setup in this class, so it can be accessed elsewhere
public static readonly string[] ab = { "ALBERT", "EINSTEIN"};
public static readonly string[] dk = { "DONALD", "KNUTH" };
//is this efficient or instantiated once per access? IF SO HOW CAN I DO THIS BETTER?
public static readonly Info AB = new Info(ab, 100);
public static readonly Info DK = new Info(dk, 80);
}
public class Info
{
private string[] _name;
private int _age;
public string[] Name { get{ return _name}; }
public int Age { get { return _age; } }
public Info(string[] n, int a)
{
_name = n;
_age = a;
}
}
If you mean the moment at when the instances of
Infoare constructed, they are built before the first access to the member. So no matters how many times you callPeople.AB, the object will be the same.From MSDN about Static Classes and Static Class Members:
If you want to make your instances of
Infototally immutable (as Daniel says, the values of_namecould be changed once you get a reference to the array), you can change the type of_nameand its property accesor to:Hope I’ve understood your needs!