36

I have a class that looks like this:

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

}

There's a lot more that I didn't show above.

Currently all of the class information is in one file. However I would like to split this into multiple files. Is there some way that I can spread the contents of the ReferenceData class across more than one file?

3 Answers 3

58

Yes, you can use partial classes. This allows you to split your class over multiple files.

File 1:

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

File 2:

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

Use this feature carefully. Overuse can make it hard to read the code.

Sign up to request clarification or add additional context in comments.

2 Comments

+1. However, consider using regions to organise the code better within a single file. If you still 'need' several files it is usually a sign that your class is not well designed. Consider breaking it into several classes with clearly defined jobs/behaviours.
@JasonWilliams Agreed but keep in mind that there are cases where partial classes are necessary, such as in asp mvc where half the class is auto-generated, and you may want to add to it.
45

Yes, include the keyword partial in the class declaration in every file where you do so.

http://msdn.microsoft.com/en-us/library/wa80x488.aspx

1 Comment

Partial classes must be having the same namespace, just an info.
12

Yes you can of course, just use the partial keyword before the class keyword at all declarations. For example, make 4 different files (but in the same namespace) containing methods and members for the ReferenceData class like this:

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

File2.cs

public static partial class ReferenceData
{

    public static IEnumerable<SelectListItem> GetDatastore()
    {
        return new[]
            {
                new SelectListItem { Value = "DEV", Text = "Development"  },
                new SelectListItem { Value = "DC1", Text = "Production" }
            };
    }
}

File3.cs

public static partial class ReferenceData
{

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

File4.cs

public static partial class ReferenceData
{

    // Lots more here
    // Lots more here
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.