1

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
        {
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
Michael Quiles
  • 1,131
  • 3
  • 24
  • 41

2 Answers2

6

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#.

Community
  • 1
  • 1
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • @Micheal: an external being (the CLR) needs to be able to find your default class to start your program. Therefore, due the need to external accessibility, it should be a public class. – Vitor Py Jul 11 '10 at 14:52
  • 1
    @Vitor, The class that has `Main` is per automatic declared public even if you do not write anything in front of it. You can't even declare it private. – Filip Ekberg Jul 11 '10 at 14:57
0
//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
  }
}
Blessy Antony
  • 113
  • 1
  • 1
  • 5