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();
}
}
}
Discussion:
17 comments Page 1 of 2.
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.
The correct answer is option B.
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.
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.
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.
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.
The programmer does not call static constructor, because static constructor implicitly called.
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".
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.
But your output is option A. Anybody, please help me out. I didn't understand.
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.
Jaya said:
10 years ago
Because first static constructor is invoke automatically no need to invoke explicitly and then static method is called.
Ajay said:
1 decade ago
Because no need call static constructor it will invoked automatically and call the static function with class name.
Sanket said:
1 decade ago
Here default constructor sample is invoked first when object is created. So it will print Sample class Bix1 method.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers