Organizing c# code into different files
I've gotten to a point where my main code file is about a thousand lines long and it's getting un-manageable; that is, I'm starting to get confused and not know where to locate some things. It's well-commented but there's just too much stuff.
I'd really like to be able to organize my code into different files, each with its own purpose. I want to get all the help VS gives me as I type when I edit these other files. A picture can say a thousa开发者_运维问答nd words:
alt text http://img64.imageshack.us/img64/7848/codeorganizationscreens.png
Is what I'm trying to do even possible?
Yes, but you need to be in the same namespace and declare the class just like you did in the main file, an example:
file1.cs
namespace Names
{
public partial class Hello
{
public void DoSomething() { }
}
}
file2.cs
namespace Names
{
public partial class Hello
{
public void Go() { DoSomething(); }
}
}
Although what other people say about partial classes
is true. I'd also suggest you to analyze refactoring opportunities on your class.
If you're having problems to manage it, you could try to split your single class in several classes with less responsibilities.
IMHO partial classes may not help very much. Do you have your class separated in regions
? Regions improve the readability of your code.
Yes you can split any partial class across as many files as you like.
- Strip out each decent size class into at least one file. Wrap each class in the same namespace.
For large classes use either:
a. Region blocks eg#region // Members
int my_int;
// other members...
#endregionb. partial keyword to break a single class accross several files.
精彩评论