Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I split my C# class across multiple files?

Tags:

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?

like image 484
Samantha J T Star Avatar asked Oct 29 '11 12:10

Samantha J T Star


People also ask

Can I split my C drive?

Once you've shrunk your C: partition, you'll see a new block of Unallocated space at the end of your drive in Disk Management. Right-click on it and choose New Simple Volume to create your new partition. Click through the wizard, assigning it the drive letter, label, and format of your choice.

How can I partition my C drive without losing data?

Cut a part of the current partition to be a new one Begin -> Right click Computer -> Manage. Locate Disk Management under Store on the left, and click to select Disk Management. Right click the partition you want to cut, and choose Shrink Volume. Tune a size on the right of Enter the amount of space to shrink.


1 Answers

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.

like image 98
Mark Byers Avatar answered Sep 21 '22 12:09

Mark Byers