C# Programming - Classes and Objects - Discussion

Discussion :: Classes and Objects - General Questions (Q.No.11)

11. 

Which of the following statements is correct about the C#.NET code snippet given below?

int i;
int j = new int();
i = 10;
j = 20; 
String str; 
str = i.ToString(); 
str = j.ToString();

[A]. This is a perfectly workable code snippet.
[B]. Since int is a primitive, we cannot use new with it.
[C]. Since an int is a primitive, we cannot call the method ToString() using it.
[D]. i will get created on stack, whereas j will get created on heap.
[E]. Both i and j will get created on heap.

Answer: Option A

Explanation:

No answer description available for this question.

Bhavin said: (Oct 3, 2012)  
The condition is that this lines must be withing some function or constructor otherwise you can not use i.ToString(); on an instance member directly in class code.

Cala said: (Oct 5, 2012)  
This code is incomplete, no constructor, no referencing and conversion can not be made so explicit.

Arun said: (May 26, 2013)  
myInt = new int(); // Invoke default constructor for int type.

This statement is equivalent to the following statement:
myInt = 0; // Assign an initial value, 0 in this example.

Using the new operator calls the default constructor of the specific type and assigns the default value to the variable. The default value of an integer is 0 BTW.


The difference is that you can't initialize and set anything but the default value value using the new operator.

Savant said: (Mar 14, 2017)  
It is perfect code snippet which creates the //default constructor//.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.