locked
Convention to put every class in its own .cs file - but what about nested classes? RRS feed

  • Question

  • User2110873642 posted

    its recommended to put each class in its own .cs file. however, what about nested classes?

    like this:

        class DayLogfile
        {
            public List<LogSet> Logs { get; set; } = new List<LogSet>();
            public class LogSet
            {
    
            }
        }

    do you then create these: ?

    • DayLogFile.cs
    • Daylogfile.LogSet.cs

    ?

    Monday, August 3, 2020 10:14 PM

All replies

  • User1535942433 posted

    Hi fazioliamboina,

    Accroding to your description,I'm guessing that you need to put nested classes into a single .cs file.I suggest you could use partial classes to split the main class and the nested classes. 

    Just like this:

    DayLogfile.cs:

    public partial class DayLogfile
      {
         // Outer class
      }

    Daylogfile.LogSet.cs:

    public partial class DayLogfile
      {
        private class LogSet
        {
          // LogSet details
        }
      }

    Best regards,

    Yijing Sun

    Tuesday, August 4, 2020 1:45 AM