C# Programming - Constructors - Discussion

Discussion Forum : 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();
        } 
    } 
}
Sample class Bix1 method
Bix1 method
Sample class
Bix1 method Sample class
Sample class Sample class
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

Bhavin said:   1 decade ago
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:   1 decade ago
Constructor can be invoked as when as object is created. Thats why default constructor is invoked first.

Greg said:   1 decade ago
The constructor is not invoked in this example.

Sanket said:   1 decade ago
Here default constructor sample is invoked first when object is created. So it will print Sample class Bix1 method.

Pooja said:   1 decade ago
In constructor static constructor always execute first.

J.harika said:   1 decade ago
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:   1 decade ago
Please anyone tell clearly why static constructor doesn't call first?

D G said:   1 decade ago
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:   1 decade ago
Because no need call static constructor it will invoked automatically and call the static function with class name.

Jaya said:   10 years ago
Because first static constructor is invoke automatically no need to invoke explicitly and then static method is called.


Post your comments here:

Your comments will be displayed after verification.