C# Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 1)
1.
Which of the following statements are true about the C#.NET code snippet given below?
String s1, s2; 
s1 = "Hi"; 
s2 = "Hi";
  1. String objects cannot be created without using new.
  2. Only one object will get created.
  3. s1 and s2 both will refer to the same object.
  4. Two objects will get created, one pointed to by s1 and another pointed to by s2.
  5. s1 and s2 are references to the same object.
1, 2, 4
2, 3, 5
3, 4
2, 5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 1 of 2.

Sangamesh said:   8 years ago
@Patricia, Your answer will be 4, but they are saying answer is [B], still am confusing can anybody knows it?

Kk dev said:   9 years ago
Its sure strings is immutable, whenever we assign new value a declared string variable it will create a new instance, previous instance is in string buffer then also it will not take that as reference becauses we lose it every time. So right answer is 4 option.

Stella said:   10 years ago
I asked some senior programmers to find explanation.

Website answers right.

String pointer s1/s2 refer to memory location with value that is located on heap.

When string value is assigned without new keyword, to avoid memory loss search is made to look up if exact same value exists on heap.

If it exists, s2 starts pointing to it too. If we wanted s2 to point to a new memory location, then we would create object using new keyword.
(1)

Patricia said:   10 years ago
Same hashCodes different objects:

string s1 = "Hi";
string s2 = String.Format("Hi");

Console.WriteLine(s1 == s2);
Console.WriteLine(s1.Equals(s2));
Console.WriteLine(object.ReferenceEquals(s1, s2));
Console.WriteLine(s1.GetHashCode());
Console.WriteLine(s2.GetHashCode());

True.
True.
False.

-839796760.
-839796760.

Nicholas Mahbouby said:   1 decade ago
Strings are a special case of object. The CLR optimizes memory allocation by interning strings with the same values. The following example will output "TrueTrueTrue".

string s1 = "Hi";
string s2 = "Hi";

Console.WriteLine(s1 == s2);
Console.WriteLine(s1.Equals(s2));
Console.WriteLine(object.ReferenceEquals(s1, s2));

Sumit said:   1 decade ago
Hi Rajeev,

MSDN says that two different strings can have same hashcode.
So, your logic is not full proof.

Any other ways anyone knows to verify the answer?

Sami said:   1 decade ago
Strings are reference type and also strings are immutable means the contents of string cannot be changed, every time when a new instance or object gets created. Here s1, s2 points to the same object.

Dinesh verma said:   1 decade ago
I agree with larry.
Strings are imutable so everytime any operation is done on strings a new object will be created so they cannot reference to the same object. The correct answer here is 4.

Rajeev said:   1 decade ago
s1 = "Hi";
s2 = "Hi";
Console.WriteLine(s1.GetHashCode());
Console.WriteLine(s2.GetHashCode());

===output===
-839796760
-839796760


Both are same object

Larry said:   1 decade ago
Strings are reference by get treated like value types being imutable. The correct answer here is 4.


Post your comments here:

Your comments will be displayed after verification.