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.

Ajay said:   1 decade ago
Because no need call static constructor it will invoked automatically and call the static function with class name.

Nikesh Rathour said:   6 years ago
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.

Sridhar said:   7 years ago
Constructor will get when we create an object but in this program not create an object why?

Sumit khedkar said:   8 years ago
No need to create object.

Ravi said:   8 years ago
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.

Anjali Rani said:   9 years ago
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.

Kirti waore said:   9 years ago
Here we not created object then how can default constructor invoke?

Vishv said:   9 years ago
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.

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

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.