A struct within a class [closed]

I have a struct lying in a class as below, which I think is not good. Structs should be in a separate file, however, in this case, this struct is only used in a class. What is the best practice for a C# struct?

class StudentMangement
{
private struct Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Grade { get; set; }
}
}

• For us to really answer this question, you really need to add some of the code from the class's methods that actually use the struct. Aug 20 '14 at 0:09
• To clarify, there is not sufficient context to comment on the core purpose of the question. Aug 20 '14 at 0:27
• Hi. Your question is on hold because we do not review "sample code" on SR. Neither do we answer general questions about design (try Programmers site for such questions). However, if you replace your sample with real code and ask if this code follows the best practices - then your question will be on-topic and can be reopened. Aug 22 '14 at 10:04
• First off, you have been told that your code is off-topic here (as it's missing context). Your question got closed accordingly. Then you proceed to edit INTO YOUR QUESTION, that this is example code (which is also off topic), Including a pseudo-rant on the policies of this site. For policy change requests please ask on Code Review Meta. I will roll back your edit. If you have more questions feel free to @ping me... Aug 22 '14 at 10:54

There's not a lot to go on here, but consider this. Can someone's age be -10? Will a student ever be 1000 years old? An int can be both of these values. That means a simple default getter/setter isn't sufficient. You'll need to write some bounds checking in the setter and throw an exception if someone tries to set Student.Age to an unreasonable number.

Having a private struct or class is fine. However, Student should not be a struct. From MSDN:

AVOID defining a struct unless the type has all of the following characteristics:

• It logically represents a single value, similar to primitive types (int, double, etc.).
• It has an instance size under 16 bytes.
• It is immutable.
• It will not have to be boxed frequently.

Is Student immutable? No, it has public setters for Name, Age, and Grade. Mutable structs lead to confusing behaviour. To quote an answer on StackOverflow

Structs are value types which means they are copied when they are passed around.

So if you change a copy you are changing only that copy, not the original and not any other copies which might be around.

Does Student represent a single value, similar to primitive types? Again, no. Things that would match this criteria include coordinates, rectangles, colours, etc.

• You should elaborate on how and why the struct in the question violates 1 or more of these points. It doesn't seem particularly obvious to me. Aug 20 '14 at 0:26