Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum with object or a dictionary-like enum

Tags:

c#

I want to create a list of names and access it as a strongly typed enum. For eg.

string foo = FileName.Hello; //Returns "Hello.txt"
string foo1 = FileName.Bye; //Returns "GoodBye.doc"

Or it could be an object like:

Person p = PeopleList.Bill; //p.FirstName = "Bill", p.LastName = "Jobs"

How do I create a datatype like this?

like image 589
Shawn Mclean Avatar asked Mar 09 '11 19:03

Shawn Mclean


People also ask

Is enum like dictionary?

Python Enum vs Dictionary Enum is used to create a set of related constants. Dictionary is used to store data values in key:value pairs. Enum is more similar to an array. The dictionary doesn't allow duplicates.

Can an enum be an object?

Because an enum is technically a class, the enum values are technically objects. As objects, they can contain subroutines. One of the subroutines in every enum value is named ordinal(). When used with an enum value, it returns the ordinal number of the value in the list of values of the enum.

Which datatype can be used with enum?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

Does enum inherit from object?

Inheritance Is Not Allowed for Enums.


1 Answers

Although the question is strange or not completely explained, here is the literal solution:

Option 1:

public static class FileName
{
    public const string Hello = "Hello.txt";
    public const string GoodBye= "GoodBye.doc";
}

Option 2:

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

    public Person(string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
    }
}

public static class PeopleList
{
    public static Person Bill = new Person("Bill", "Jobs");
}
like image 56
Josh M. Avatar answered Oct 01 '22 23:10

Josh M.