用枚举对象或一个类似于字典的枚举 [英] Enum with object or a dictionary-like enum

查看:125
本文介绍了用枚举对象或一个类似于字典的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建名和访问它作为强类型枚举的列表。对于例如:

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?

推荐答案

虽然这个问题很奇怪或不完全解释,这里是字面的解决方案:

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

选项1:

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



选项2:

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");
}

这篇关于用枚举对象或一个类似于字典的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆