public class myclass
{
public Details DetailsInfo { get; set; }
public string name{ get; set; }
public string Email { get; set; }
public int subjects{ get; set; }
}
public enum subjects{
maths,
english,
science,
}
Among these subjects is an enum. Even if I don't enter any value for subjects it takes 0 by default. This is the behavior of enum. Is there any way to check if I have chose any value for subject.
Note: I don't want to any value like undefined in the enum.
You could solve the issue in two ways.
First, define a None
value (or whatever you want to name it) which represents 0
to indicate there is nothing chosen for an enum
value:
public enum subjects{
None = 0, //define this
maths,
english,
science,
}
Alternatively, use Nullable<subjects>
or subjects?
in your class ( myclass
).
public class myclass
{
public Details DetailsInfo { get; set; }
public string name{ get; set; }
public string Email { get; set; }
public subjects? subj { get; set; } //note the question mark ?
}
Of the two methods, the first one is more common. Thus I would rather use the first one.
You can change the class to look like this:
public class myclass
{
public Details DetailsInfo { get; set; }
public string name{ get; set; }
public string Email { get; set; }
public subjects? subject { get; set; }
}
So that the subject is nullable. It means that when you have not set it. It will be null
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.