Can we seperate the functions in different files?
-
I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".
-
I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".
-
I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".
You can have the same class split over multiple files (from .NET 2 on), by using the
partial
keyword. It doesn't matter what the filenames are (before the .cs that is), because the compiler will combine them together. However, if you have too many functions then this would tend to indicate that there may be a problem with your design. You may be linking too many things together that really should be separated. Anyway here's a quick example:FirstFile.cs public partial class MyClass { // Do something. } SecondFile.cs public partial class MyClass { // Do something else. }
Interestingly enough, you could create a member in FirstFile.cs and it would be available in SecondFile.cs. This behaviour is visible in the way that Visual Studio.Net breaks up form files.
Deja View - the feeling that you've seen this post before.
-
I have too many functions in one class, therefore, I want to know is it possible to seperate the functions into seperate files? So far, I only know one class is saved as one file ".cs".
As a best practice it is advised to refractor functions that do similar things in different classes. That way you avoid having problem with too much code in the same .cs file. In my case I choose to have at most 400 lines of code in a class.
I am fighting against the Universe... Reference-Rick Cook