-2

I was asked this question in my examination

class main
{
    static long afield = 123;

    static main()
    {
        Console.WriteLine(afield);
    }

    main()
    {
        afield = 1000;
        Console.WriteLine(afield);
    }
}

static void Main(String[] args)
{
    main obj = new main();
}

What is the output

  • a) 1000123
  • b) 123
  • c) No output
  • d) 1231000

I check a) and it was wrong.

When I ran the program, I got an error stating that ConsoleApplication1.main.main() is inaccessible due to its protection level.

Aren't constructors supposed to be public by default? Why was I getting this error?

VRM
  • 151
  • 2
  • 11

5 Answers5

4

Omitting the access modifier (public, private, internal or protected) on any member (constructor, field, property, method, event, etc.) will leave the member private by default.

However if you don't define any constructors yourself, the class will implicitly get a parameterless public constructor. If this weren't so, you would have to write an empty public constructor for every class you create.

vgru
  • 49,838
  • 16
  • 120
  • 201
2

Default Constructor (constructor without parameters) is public by default but in your code you make your default constructor PRIVATE. So they are inacesible in other part of code.

   class main
{
    static long afield = 123;

    public static main()
    {
        Console.WriteLine(afield);
    }

   public main()
    {
        afield = 1000;
        Console.WriteLine(afield);
    }
}

static void Main(String[] args)
{
    main obj = new main();
}

Make it public and everything will be allright.

In this case C is good answer becouse your program will throw exception.

Ridikk12
  • 406
  • 1
  • 4
  • 13
  • Your definition of 'default constructor' is wrong. The number of parameters has no effect on the access level. An autogenerated constructor (where you just don't write any ctor) is public. – H H Mar 13 '17 at 11:10
  • So, for example if i declare as private parameterless constructor as this: private ctror(){} -> this isn't default constructor ? I have been thinking that defualt constructor is this one without parameters. So private ctor(){} and public ctor(){} both are default constructor. One is inaccessible outside the class and other is. – Ridikk12 Mar 14 '17 at 17:10
1

Constructors given without an access modifier will be private be default, hence the inaccessibility error you were getting.

0

To at least answer why the answer to to exam question was not 'a', there are a couple of details:

  1. The access modifier on the constructor was omitted (omitted access modifiers on members default to private). This throws an exception resulting in 'No Output' or 'c'

  2. If the access modifier on the constructor had been public, the static constructor would have fired first resulting in the output being '123100' or 'd'.

-2

In order to access your class you have to explicitly mark it as public then only you will be able to access the class out side Hope it helped