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 2 of 2.

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

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

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

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

Preethi said:   1 decade ago
Constructor can be invoked as when as object is created. Thats why default constructor is invoked first.

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.


Post your comments here:

Your comments will be displayed after verification.