- Home /
Splitting a code file into multiple files
Suppose you have one c# script, abc.cs, which is very long .. 100s of lines. Is there a way to organize it somehow? Thanks!
Answer by roburky · Aug 14, 2011 at 08:28 PM
If you are using C# you can split one class into separate files if that's easier for you to work with.
In each file, put the word "partial" before the word "class", and add descriptions to your file names after a period.
For example: "MyClass.cs", "MyClass.Attack.cs", "MyClass.Flee.cs".
public partial class MyClass { }
Reference:
Note that "all parts" must have the partial keyword.
(So, you don't just add it to the "others", you put it on the "first" one and all the others as well.)
Answer by DaveA · Aug 14, 2011 at 05:03 PM
AHHG! Don't copy and paste!
We're dealing with object-oriented code here, this is the perfect case for using subclasses. You can put more that one class in a file, or have each in separate files. But all functions for a single class should go in the same file.
For anyone reading this old QA it's totally ubiquitous to split large c# files using "partial". (This does not contradict the general program$$anonymous$$g principle that no class should be longer than 50 or so lines; but in quite a few cases it's normal and natural to have "one very long class".)
Your answer
