我可以将我的C#类拆分为多个文件吗?

我可以将我的C#类拆分为多个文件吗?,c#,C#,我有一个类似这样的类: public static class ReferenceData { public static IEnumerable<SelectListItem> GetAnswerType() { return new[] { new SelectListItem { Value = "1", Text = "1 answer" }, new

我有一个类似这样的类:

public static class ReferenceData
{

    public static IEnumerable<SelectListItem> GetAnswerType()
    {
        return new[]
            {
                new SelectListItem { Value = "1", Text = "1 answer"  },
                new SelectListItem { Value = "2", Text = "2 answers" },
                new SelectListItem { Value = "3", Text = "3 answers" }
            };
    }

    public static IEnumerable<SelectListItem> GetDatastore()
    {
        return new[]
            {
                new SelectListItem { Value = "DEV", Text = "Development"  },
                new SelectListItem { Value = "DC1", Text = "Production" }
            };
    }
    public static string GetDatastoreText(string datastoreValue)
    {
        return GetDatastore().Single(s => s.Value == datastoreValue).Text;
    }
    public static string GetDatastoreValue(string datastoreText)
    {
        return GetDatastore().Single(s => s.Text == datastoreText).Value;
    }

    // Lots more here
    // Lots more here

}
公共静态类引用数据
{
公共静态IEnumerable GetAnswerType()
{
返回新的[]
{
新建SelectListItem{Value=“1”,Text=“1 answer”},
新建SelectListItem{Value=“2”,Text=“2答案”},
新建SelectListItem{Value=“3”,Text=“3答案”}
};
}
公共静态IEnumerable GetDatastore()
{
返回新的[]
{
新建SelectListItem{Value=“DEV”,Text=“Development”},
新建SelectListItem{Value=“DC1”,Text=“Production”}
};
}
公共静态字符串GetDatastoreText(字符串datastoreValue)
{
返回GetDatastore().Single(s=>s.Value==datastoreValue).Text;
}
公共静态字符串GetDatastoreValue(字符串datastoreText)
{
返回GetDatastore().Single(s=>s.Text==datastoreText).Value;
}
//这里还有很多
//这里还有很多
}
上面我没有展示的还有很多


目前所有的类信息都在一个文件中。但是,我想将其拆分为多个文件。是否有某种方法可以将ReferenceData类的内容分散到多个文件中?

是的,在您这样做的每个文件的类声明中都包含关键字
partial

是的,您可以使用。这允许您将类拆分为多个文件

文件1:

public static partial class ReferenceData
{
    /* some methods */
}
文件2:

public static partial class ReferenceData
{
    /* some more methods */
}

请小心使用此功能。过度使用会使代码难以阅读。

是的,当然可以,在所有声明中,只需在
关键字之前使用
部分
关键字。例如,创建4个不同的文件(但在同一命名空间中),其中包含
ReferenceData
类的方法和成员,如下所示:

public static class ReferenceData
{

    public static IEnumerable<SelectListItem> GetAnswerType()
    {
        return new[]
            {
                new SelectListItem { Value = "1", Text = "1 answer"  },
                new SelectListItem { Value = "2", Text = "2 answers" },
                new SelectListItem { Value = "3", Text = "3 answers" }
            };
    }

    public static IEnumerable<SelectListItem> GetDatastore()
    {
        return new[]
            {
                new SelectListItem { Value = "DEV", Text = "Development"  },
                new SelectListItem { Value = "DC1", Text = "Production" }
            };
    }
    public static string GetDatastoreText(string datastoreValue)
    {
        return GetDatastore().Single(s => s.Value == datastoreValue).Text;
    }
    public static string GetDatastoreValue(string datastoreText)
    {
        return GetDatastore().Single(s => s.Text == datastoreText).Value;
    }

    // Lots more here
    // Lots more here

}
File1.css

public static partial class ReferenceData
{

    public static IEnumerable<SelectListItem> GetAnswerType()
    {
        return new[]
            {
                new SelectListItem { Value = "1", Text = "1 answer"  },
                new SelectListItem { Value = "2", Text = "2 answers" },
                new SelectListItem { Value = "3", Text = "3 answers" }
            };
    }
}
File4.cs

public static partial class ReferenceData
{

    // Lots more here
    // Lots more here
}

但是,考虑在单个文件中使用区域来更好地组织代码。如果您仍然“需要”几个文件,这通常表明您的类设计得不好。考虑把它分解成几个具有明确定义的工作/行为的类。@ Jason Wrimes同意,但请记住,有些情况下需要部分类,例如在ASP MVC中,其中一半的类是自动生成的,并且您可能想添加它。部分类必须具有相同的命名空间,只是信息。
public static partial class ReferenceData
{

    // Lots more here
    // Lots more here
}