开发者

Trying to break apart files to make code easier to read; any shortcuts to avoid excessive indentation?

开发者 https://www.devze.com 2023-02-04 19:55 出处:网络
I\'m working on a project where we\'re nesting somewhat complex classes within other classes, and it has come to the point where breaking these up into separate files may help us maintain some sort of

I'm working on a project where we're nesting somewhat complex classes within other classes, and it has come to the point where breaking these up into separate files may help us maintain some sort of order over the mess.

But, with a single nested class, even if a file contains nothing else (no other classes), I'm still having to prefix each line with three levels of indentation, which makes hitting the limits of horizontal readability too easy, even with simple statements (albeit many with long function/class/variable names).

The file might look like this:

namespace Example.Namespace1
{
    public partial class ImportantClass
    {
        protected partial class Nested1
        {
            // I can finally start writing code here
            public int AddOffset(int offset)
            {
                // Code inside of a me开发者_StackOverflow社区thod
            }

            public string ID{ get; protected set; }
        }
    }
}

So, do I have any shortcuts to declaring Nested1 that can save me some horizontal space (e.g. protected partial class ImportantClass.Nested1 doesn't work, but something like that)?


Don't do it.

Use the class nesting sparingly just for simple internal use classes. The fact that you are trying to split them out into different files tells you something - ie that you have gone a bit overboard with the nested classes.

Using them publicly out side of your code is ugly ugly.

If you need to keep things internal use the internal modifier. Name spaces and project folders can help you keep everything organized.


It sounds like the answer is No.

We have some alternate approaches here, but to the answer to the question: Does C# have any shorthand notation for nested classes? No, it doesn't.

0

精彩评论

暂无评论...
验证码 换一张
取 消