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 2 of 2.

Manish Kumar said:   9 years ago
@Andrew Cope.

I agree its better to say "The characteristics of the two objects will be the same".

Alek said:   9 years ago
This is wrong, for example if Student is defined as:

class student
{
static int _last_id;
static Student () { _last_id = 0; }
int id;
student ()
{
id = _last_id;
_last_id++;
}
}

Then obviously their contents won't be the same.
The correct answer is : 'Program won't compile'.
Because keyword class is illegal in a statement.

John said:   9 years ago
@Alek.

Yes same thoughts here, the code given won't compile, because the class keyword is used before the declaration of the 2 student objects s1, s2.

Sidhu said:   9 years ago
BaseClass s1,s2;// = new Drived();
s1 = new BaseClass();
s2 = new BaseClass();
if (s1 == s2)
{
Console.WriteLine("contents of both objects are same");
}

GardenExit said:   8 years ago
@Indrapal.

s1 and s2 have different hashcodes.
So,
s1 == s2
It can never be true.

Dickenz said:   7 years ago
A: S1 and S2 are merely reference variables containing the addresses of the 2 objects created therefor contents are different.

B: reference type variables are created on the heap not stack.

D: objects created on the heap are allocated memory at run time such that they are not necessarily created in adjacent memory.

E: deleting from memory is done by garbage collection or explicitly defining a despose method using methods such as GC.SuppressFinalize.

Monk said:   7 years ago
class Student s1, s2;

Can anyone explain what is s1,s2 here, whether is it class or variable?


Post your comments here:

Your comments will be displayed after verification.