C# Programming - Strings - Discussion

Discussion :: 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.

[A]. 1, 2, 4
[B]. 2, 3, 5
[C]. 3, 4
[D]. 2, 5

Answer: Option B

Explanation:

No answer description available for this question.

Swati said: (May 18, 2011)  
HOW TO SOLVE THIS PROBLEM

Larry said: (Sep 7, 2011)  
Strings are reference by get treated like value types being imutable. The correct answer here is 4.

Rajeev said: (Oct 25, 2011)  
s1 = "Hi";
s2 = "Hi";
Console.WriteLine(s1.GetHashCode());
Console.WriteLine(s2.GetHashCode());

===output===
-839796760
-839796760


Both are same object

Dinesh Verma said: (Jun 10, 2012)  
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.

Sami said: (Jan 4, 2014)  
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.

Sumit said: (Mar 28, 2014)  
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?

Nicholas Mahbouby said: (May 7, 2014)  
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));

Patricia said: (Dec 16, 2014)  
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.

Stella said: (Mar 11, 2015)  
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.

Kk Dev said: (Mar 17, 2016)  
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.

Sangamesh said: (Oct 26, 2016)  
@Patricia, Your answer will be 4, but they are saying answer is [B], still am confusing can anybody knows it?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.