V
V
Victor P.2016-07-27 10:03:07
.NET
Victor P., 2016-07-27 10:03:07

How to properly store c# structure?

Good afternoon. The question of the organization of memory and how it is generally more correct to do it. So, there is some data structure

public struct myStruct
    {
        public myEnum id;
        public string field1;
        public string field2; 
        public List<string> fields3;
     }

It doesn't matter what the fields are and omitted the constructor. There are a limited number of these structures and access to them is needed from the entire application. In fact, this is a kind of reference book, which, for speed, we store not in the database, but directly in the code. There are two ways to organize, the first is to wrap access to such structures in a switch branching function
public static myStruct Get(myEnum id)
{
     switch (id)
            {
                case myEnum.First: return new myStruct(. . . );
                case myEnum.Second: return new myStruct(. . . );
                default: return null;
            }
}
//вызываем так
var result = Get(myEnum.first);

And the second option, store in a dictionary
public static class pogreb
    {
        public static Dictionary<myEnum, myStruct> spravochnik;

        public static void fillSpravochnik()
        {
            var res =  new Dictionary<myEnum, myStruct>();
            res.Add( myEnum.first, new myStruct(. . .) );
            res.Add( myEnum.second, new myStruct(. . .) );
            spravochnik = res;
        }
    }
//Ну и вызывать, соответственно
if(pogreb.spravochnik == null) pogreb.fillSpravochnik();
var result = pogreb.ContainsKey(myEnum.first) ? pogreb[myEnum.first] : null;

That is, in the first case, we create a structure with each call and give it back as a result, and in the second case, we create structures once and store all the results in a directory. Which approach to use? Advantages and disadvantages? Is this correct and is there any other approach?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fat Lorrie, 2016-07-27
@Jeer

Looks like a "hardcode vs container" comparison. The difference is obvious: Dictionaryit provides search with complexity O(1)(against O(n)the switch) and is able to expand dynamically.
In the context of the task, it is like DI for the poor. In this case, the Dictionary will be clearer.
It would not be superfluous to read the style guide for C #

M
Maa-Kut, 2016-07-27
@Maa-Kut

Since we are talking about structures, the second option does not make sense, because. copies of the structures stored in the dictionary will still be returned from the dictionary: in C#, structures are value types.
Well, for the future: if we go the second way, then we need to use encapsulation more actively. Those. initialization of the spravochnik field and getting a value from it must be done directly in the pogreb class . Outside, the class should expose not a public field (public fields are almost always evil), but a method or property that will check / initialize sparovochnik and look for a value in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question