C# Programming - Constructors - Discussion

Discussion Forum : Constructors - General Questions (Q.No. 12)
12.
Which of the following statements are correct about the C#.NET code snippet given below?
class Sample
{
    static int i;
    int j;
    public void proc1()
    {
        i = 11; 
        j = 22;
    }
    public static void proc2()
    {
        i = 1;
        j = 2;
    }
    static Sample()
    {
        i = 0; 
        j = 0;
    }
}
i cannot be initialized in proc1().
proc1() can initialize i as well as j.
j can be initialized in proc2().
The constructor can never be declared as static.
proc2() can initialize i as well as j.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
8 comments Page 1 of 1.

Suresh.D said:   8 years ago
Proc1 and Proc2 are not constructors, acting as methods, proc1 is a normal method and proc2 is static method static method accepts on static fields. So that proc2 can not initialize j, only proc1 can initialize both i and j as it can accept both static and non-static fields. Similar static constructor sample also cannot initialize j as it is static. So B is the right answer.

Rohan said:   10 years ago
I think option B is not Correct because I am copy of code in vs10. But there other kind of problem like as static Sample class give compile error method must return type and int j is not static variable, that's why give compile error in static method.
(1)

Shafi said:   1 decade ago
static Sample()
{
i = 0;
j = 0; //it is a instance variable
}

In static constructor we can initialize only static variables.

ERROR

Ravikiran said:   1 decade ago
Constructors should not have any return type atleast void also.

Harish said:   1 decade ago
This question is wrong, because they should ask which option is not correct.

ANGUS said:   1 decade ago
I want to know more about JAVA programming.

PriyankaD said:   1 decade ago
It is complie time error because when you declare static constructor then class act as static class then all member must be static but here j is not static.

Sadiq said:   1 decade ago
Static constructors can have only static data members to be intialised. Since proc1 is non staic function it can intialise both static and non static data members of sample class. proc2 is a static function of class sample it can not intialise non static data members of sample class.

Post your comments here:

Your comments will be displayed after verification.