C# Programming - Classes and Objects - Discussion

Discussion Forum : Classes and Objects - General Questions (Q.No. 10)
10.
Which of the following statements are correct about the C#.NET code snippet given below?
sample c;
c = new sample();
  1. It will create an object called sample.
  2. It will create a nameless object of the type sample.
  3. It will create an object of the type sample on the stack.
  4. It will create a reference c on the stack and an object of the type sample on the heap.
  5. It will create an object of the type sample either on the heap or on the stack depending on the size of the object.
1, 3
2, 4
3, 5
4, 5
None of these
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 1 of 2.

Jagan said:   7 years ago
Objects contents are always created on the heap and its reference on the stack.

Vinay said:   9 years ago
First the right part of sign is executed i.e new sample() which is nameless object & then it's reference is assigned to C.

Aarti said:   9 years ago
How come 'nameless' object is created when we have named object as 'c'?
(1)

Nicholas Mahbouby said:   10 years ago
I agree with @Johan.

The name of the object is C. Only for reference types does the CLR create a nameless object on the heap and a named value type that contains it's address. From the code snippet it is not possible to tell if the type "sample" is a reference type or a value type. It could be a class or a struct.

Taylor said:   1 decade ago
I agree with Silky, but just wonder why they say it's a nameless object?

Taylor said:   1 decade ago
I agree with Silky, but just wonder why they say it's a nameless object?

Sindujadevi said:   1 decade ago
We are creating object to class so I think this is the reference type and it will store in heap.

Johan Braanen said:   1 decade ago
Both 2 and 4 are wrong.
2: The name of the object is "c"
4: We don't know if the objects is placed on the heap, because we don't know If it is a reference type or a value type.
(1)

Gopesh said:   1 decade ago
Why it is saying nameless object, since name of the object is 'c' only ?

Silky Malik said:   1 decade ago
@pooja objects' contents are always created on heap and its reference on stack.


Post your comments here:

Your comments will be displayed after verification.