4

With a Person class, how do I go about representing Gender, I want to create it as a class, much like the System.Drawing.Color setup, but I am unsure how to do this.

So I can do Gender = Gender.Male or Gender = Gender.Female etc.

Sorry if this makes no sense.

4
  • Do some reading about enums in C#.
    – adv12
    Feb 15, 2017 at 20:57
  • You are probably looking for enumerations..
    – Geeky Guy
    Feb 15, 2017 at 20:57
  • Can i not create it as a class, and also a static class with the options, like Drawing.Color?
    – F17CH
    Feb 15, 2017 at 20:59
  • 1
    You might want to follow ISO/IEC 5218. The correct term is "sex", not "gender", although "political correctness" is trying to pervert the meanings. However, language does evolve. Feb 15, 2017 at 21:02

1 Answer 1

8

You could create an enum:

public enum Gender { Male, Female }

and then use it as below:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Gender Gender { get; set; }
}

var person = new Person
{
    FirstName = "Foo",
    LastName = "Bar",
    Gender = Gender.Male
};
7
  • 4
    That is not politically correct. How do you represent shemale? Or Apache Helicopter? Feb 15, 2017 at 20:58
  • Where is the Gender enumeration created? In the Person class?
    – F17CH
    Feb 15, 2017 at 21:02
  • It's up to you. In the Person class is one option.
    – Nick
    Feb 15, 2017 at 21:03
  • @bc004346 I mentioned the values Male and Female, since these values have been mentioned by the OP. Of course anyone can add as many values in this enum provided that these values make sense for her application.
    – Christos
    Feb 15, 2017 at 21:04
  • 2
    @F17CH You should add a file called in Gender.cs to your project. Then you should add the definition of the enum there, public enum Gender { Male, Female }. Marking the enum as public you state that every type in your assembly or in any other assembly that references your assembly would have the opportunity to access and use your enum. I hope that I answered your question. If not, please let me know. Thanks !
    – Christos
    Feb 15, 2017 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.