Make the response class a structure?

Usually play with games, but I'm going to combine a few questions and answers for educational purposes.

In any case, I have a class of questions that contains many members: the question itself, the answer to the question , an array of possible answers, etc. There is no doubt that this should be a class.

However, my response class only contains a string, an Enum number, and an int id, as shown below:

public class Answer { public string Answer { get { return answer;} private set { answer = value; } } public Answer_Category = Some_Value; // The enum. public int ID { get { return id; } private set { return id; } } private string answer; private int id; } 

Ok, so it contains two lines as well as ctor.

So, should I make this a structure? I ask how this seems comparable to creating Vector a struct, which is such a small data structure "n all".

Naturally, as a question and answer, the answer of the class / structure will be the subject of many search calls.

IMO it should be a structure - just because of the size of the structure, have not played with C # for some time, although it was just looking for some explanation.

+7
c # struct class
source share
2 answers

The solution comes down to whether you want a value type or a reference type. In other words, what do you want the assignment operator to mean? If you want the assignment to copy the value, use struct. If you want to assign a different e link, use the class.

+5
source share

There are two main uses for structures: for situations where it is convenient to move around aggregates of independent variables (such as point coordinates) or where a small immutable object is often used in situations where identity is not important (as in the case of, for example, Decimal or DateTime ) . Because a structure type variable simply contains an aggregation of variables, the structures that are used for this purpose should simply expose their contents as public fields. It will be clear that any invariants that may be applicable to members of the structure will be responsible for the code that creates the structure. For example, if you have a MinMax structure with int members Minimum and Maximum , using open public fields for these members will make it clear that the structure itself does not make any promises that Maximum >= Minimum . A method that returns such a structure may promise that it will not return to where Maximum < Minimum , but methods that receive structures and want Maximum >= Minimum will have to check these fields themselves. On the other hand, a code that wants to set these values ​​in a structure will be able to set them in any order, without having to worry that the structure has thrown an exception because Minimum been set higher than Maximum .

The MSDN guidelines suggest that the goal of any structure is to act like an object. Because piecewise mutable structures do not behave like objects, structures that will be used as objects should not be piecewise modified. On the other hand, clusters of variables can be useful on their own; you should use a structure in cases where aggregation of variables is required, but you should try to make it as obvious as possible that the structure is an aggregation of variables, not an object.

+1
source share

All Articles