C# Programming - Classes and Objects - Discussion

Discussion Forum : 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();
This is a perfectly workable code snippet.
Since int is a primitive, we cannot use new with it.
Since an int is a primitive, we cannot call the method ToString() using it.
i will get created on stack, whereas j will get created on heap.
Both i and j will get created on heap.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
5 comments Page 1 of 1.

Arun said:   1 decade ago
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.

Bhavin said:   1 decade ago
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.
(1)

Cala said:   1 decade ago
This code is incomplete, no constructor, no referencing and conversion can not be made so explicit.

Savant said:   8 years ago
It is perfect code snippet which creates the //default constructor//.

Mahmoud Hamed said:   3 years ago
@All.

int x = new int();

=> new gives x default value of x.

Post your comments here:

Your comments will be displayed after verification.