Tutorial :Clean code which version is correct?



Question:

When the only code in there is for a class how would you code it adding public to the default class like so

namespace HW2_2_Spaceship  {     public class Spaceship //added public to the default class       {        static void Main(string[] args)          {          }  

or

namespace HW2_1_Book  {      class Book      {          static void Main(string[] args)          {            }          public class Book // added a new class with in the default class          {  


Solution:1

In general, each class should have their own file.

Main should be in Program.cs

There are usecases where you can use Inner classes, see Using Inner classes in C#.


Solution:2

//Program.cs, if u use visual studio then ensure you add  // the public access modifier yourself    namespace HW2_2_Spaceship  {     public class Program     {        public static void Main(string[] args)        {          //Do something here        }     }  }    //Book.cs, add the public modifier to the class  namespace HW2_2_Spaceship  {    public class Book    {     //add method and properties here    }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »