C# Programming - Constructors - Discussion

Discussion :: Constructors - General Questions (Q.No.16)

16. 

What will be the output of the C#.NET code snippet given below?

namespace IndiabixConsoleApplication
{ 
    class Sample
    { 
        static Sample()
        { 
            Console.Write("Sample class ");
        }
        public static void Bix1()
        { 
            Console.Write("Bix1 method ");
        } 
    } 
    class MyProgram
    { 
        static void Main(string[ ] args)
        { 
            Sample.Bix1();
        } 
    } 
}

[A]. Sample class Bix1 method
[B]. Bix1 method
[C]. Sample class
[D]. Bix1 method Sample class
[E]. Sample class Sample class

Answer: Option A

Explanation:

No answer description available for this question.

Bhavin said: (Oct 2, 2012)  
Because whenever you call some static method, If that class instance is not already created, it create an instance and then execute static method.

Preethi said: (Jun 25, 2013)  
Constructor can be invoked as when as object is created. Thats why default constructor is invoked first.

Greg said: (Aug 5, 2013)  
The constructor is not invoked in this example.

Sanket said: (Oct 11, 2013)  
Here default constructor sample is invoked first when object is created. So it will print Sample class Bix1 method.

Pooja said: (Oct 21, 2013)  
In constructor static constructor always execute first.

J.Harika said: (Dec 21, 2014)  
I think the answer is wrong. Here no need to create object. Not creating object means constructor is not invoked. In order to call static method we required class name. Classname.Methodname();

The correct answer is option B.

Usha said: (Dec 23, 2014)  
Please anyone tell clearly why static constructor doesn't call first?

D G said: (Mar 23, 2015)  
No need for calling as class name, the method as static, and main also static, so no need to specify the sample.Bix(), Bix(); is more enough. The o/p is "Bix 1 method".

Ajay said: (Apr 14, 2015)  
Because no need call static constructor it will invoked automatically and call the static function with class name.

Jaya said: (Oct 22, 2015)  
Because first static constructor is invoke automatically no need to invoke explicitly and then static method is called.

Vishv said: (Aug 2, 2016)  
Ans- Sample class Bix1 method.

The entry point of the program is main().

When we write a static constructor, this is the next block of code execute implicitly after main() either you call it or not.

Kirti Waore said: (Sep 16, 2016)  
Here we not created object then how can default constructor invoke?

Anjali Rani said: (Jan 17, 2017)  
I have run the same code but I'm getting output: Sample class only.

But your output is option A. Anybody, please help me out. I didn't understand.

Ravi said: (May 30, 2017)  
The Constructor will call at the time of object creation only and static method will call at the time of compilation. So here we are not create any object for constructor so the output is sample class only.

Sumit Khedkar said: (Oct 1, 2017)  
No need to create object.

Sridhar said: (Jun 10, 2018)  
Constructor will get when we create an object but in this program not create an object why?

Nikesh Rathour said: (Sep 23, 2019)  
Static constructor first block of code which always executes first even main method.

The programmer does not call static constructor, because static constructor implicitly called.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.