Question

I have a Window Form App project. At the moment all of my code is in Form1.cs file which is the default file. Now I have about 1300 lines of code in this single file. I want to break down this one file code into several files to increase readability and easy handling. Is there an way to do that.

Thanks

Was it helpful?

Solution 3

If you do not want to do anything drastic you can split your class with the partial keyword and place each piece in its own file.

public partial class Form1 { // file1.cs }
public partial class Form1 { // file2.cs }

See http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx

OTHER TIPS

Use Refactor. Select the part of code right click with mouse -> Refactor->Exctract method. Or Ctrl+M combination.

Yes, absolutely. You can use Partial classes to achieve this.

And this is not only about readability of your code, cause it can be even in single file (it depends I mean). But splitting the same code between different files will let you be much more flexible in group development when you're using source control.

So multiple team members can work on the same Form class, but as they work on different IO artifacts (files), that were devided based on some logic, they will not jump into the conflicts (as much as it possible).

Think about this too.

It is usually not a good practice to put all your code into a single class. I recommend you to apply the Separation of Concerns principle and break up that single class into multiple ones, according to their concerns.

http://en.wikipedia.org/wiki/Separation_of_concerns

About organizing code: usually people organize code in such a way that 1 file contains 1 class. If that class is too big (and cannot be torn apart), you can still use C#'s partial class feature to organize the same class into multiple files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top