tags:

views:

114

answers:

2

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
        {
+6  A: 

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

Filip Ekberg
thank you very much
Michael Quiles
@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
@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
@Ekberg Good to know. Thanks :)
Vitor Py
A: 
//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
You don't need to add the public modifier on the class that has `Main` in fact it will always be public, you cannot set it to private / protected.
Filip Ekberg
I prefer access modifiers to be specified explicitly.
Blessy Antony