C# Programming - Classes and Objects - Discussion

Discussion Forum : Classes and Objects - General Questions (Q.No. 1)
1.
Which of the following statements is correct about the C#.NET code snippet given below?
class Student s1, s2; // Here 'Student' is a user-defined class.
s1 = new Student(); 
s2 = new Student();
Contents of s1 and s2 will be exactly same.
The two objects will get created on the stack.
Contents of the two objects created will be exactly same.
The two objects will always be created in adjacent memory locations.
We should use delete() to delete the two objects from memory.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 1 of 2.

G.SRIRAM said:   1 decade ago
No issue.

A default constructor with dynamic allocation of objects is created a default constructor accepts no parameters. So the constructors that are created will have same characteristics.

Sundar said:   1 decade ago
@G.Sriram

What if the constructor assigns random values to the member variables of the instance of the class ?

Paurush said:   1 decade ago
What is the differnce b/w option A and C ?
Please clarify the solution.

Indrapal said:   1 decade ago
I do not think compiler will assign the content to both object and I tried to VS2010 and show that objects has the different content
BaseClass s1,s2;// = new Drived();
s1 = new BaseClass();
s2 = new BaseClass();
if (s1 == s2)
{
Console.WriteLine("contents of both objects are same");
}

Kanika said:   1 decade ago
In terms of memory allocated to s1 and s2 they are equal i.e.
All objects created from a class will occupy equal number of bytes in memory.

Mayank Srivastava said:   1 decade ago
I am agree with Indrapal. I have also tried it and it shows that objects has the different content.

Naresh said:   1 decade ago
@Indrapal.

s1 and s2 contains the address that where the content is stored.

So s1 and s2 are stored at diff memory locations so obviously if block won't execute.

@Paurush.

Exactly same means including addresses of the objects,
But its not true as I explained above,
So the answer is C only.

Vaiju said:   1 decade ago
s1 and s2 contains the address that where the content is stored.

So s1 and s2 are stored at diff memory locations so obviously .

Andrue Cope said:   1 decade ago
Yuk, sneaky question :)

I think a lot of C# programmers will assume that 'the contents of s1' mean the object pointed to it. I did. Have to say that it's more clear if you rewrite in C++ :D

Andrew Cope said:   10 years ago
Slightly sneaky indeed. It shouldn't say 'Contents of..exactly the same'. It would be more correct to say 'The characteristics of the two objects will be the same'.

That covers off the scenario mentioned above where member data is being initialised to random or otherwise variant values.


Post your comments here:

Your comments will be displayed after verification.