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?
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.
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.
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.
Inheritance Is Not Allowed for Enums.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With