Discussion :: Strings - General Questions (Q.No.3)
Mark said: (May 12, 2012) | |
String s1 = "String"; String s2; s2 = String.Copy(s1); //is the right answer!! |
Vishnu said: (Nov 16, 2012) | |
String s1 = "String"; String s2; s2 = s1; //it is also right |
Neeraj said: (Jul 12, 2013) | |
Mark is right. Because in s2=s1 giving the reference of s1 to s2. |
Amit Gupta said: (Jul 24, 2013) | |
Why not first is right? |
Zeel said: (Jul 27, 2013) | |
Yes exactly this is also right. String s1 = "String"; String s2; s2 = s1; //It is also right. |
Mahi said: (Dec 21, 2013) | |
Can't find the difference. Can anyone tell clearly. |
Sami said: (Jan 4, 2014) | |
When s1="string"; here a reference points to the "string". and when. s2=s1; here s2 also points to the "string". and same is the case. When s2 = String.Copy (s1) ;. So why 1st is not right. Can any please tell the difference? |
Nichholas Mahbouby said: (May 7, 2014) | |
s1 = s2 will result in 2 variables that reference the same object. s2 = string.Copy(s1) will result in 2 variables that reference different objects containing the same value. Console.Write(object.ReferenceEquals(s1, s2)) will output "False". |
Max said: (May 12, 2014) | |
Desription: Acc. to question we need to copy the contents of one string into another. Solution:- Option-1 : Incorrect, String s1 = "String" when executed will create a "String" object. s2 = s1 doesn't copies the content rather it passes its reference to variable s2. Only 1 Object is created both s1 and s2 referring same object. Option-2 : Obviously Incorrect answer, Reason Strings are immutable Option-3 : Correct, because here we are only copying the content , so in end we will have 2 objects . By value they are same but by reference they are different. Option-4 : No overload for method 'Replace' takes '0' arguments. Option-5 : Type 'string' does not contain a definition for 'StringCopy' and no extension method 'StringCopy' of type 'string' could be found. Thank You :). |
Vijay said: (Jul 31, 2014) | |
If you refer to the first question in strings, it clearly mentions strings are immutable and CLR optimizes memory by interning same strings into one. If Option 3 is correct, does it not defeat the above fact ? |
Dheer said: (Dec 25, 2014) | |
String s2, s1 = "string"; s2 = s1; s2.equals(s1); |
Nasser Amira said: (Jan 19, 2015) | |
Awesome discussion. In response to @Nichholas, when you run the following the console return true actually not false. String s1 = "String"; String s2; s2 = s1; Console.Write(object.ReferenceEquals(s1, s2)); |
Pankaj Mehta said: (Sep 15, 2015) | |
Case A one references got copied. Mean both string now got started to point same [Memory]. Case C exact data is copied in other string. |
Naresh said: (Jul 16, 2016) | |
Will get same object name. String s1 = "String"; String s2; s2 = String.Copy(s1); Console.WriteLine(s2); Console.WriteLine(s1); Console.WriteLine(s1.GetHashCode()); Console.WriteLine(s2.GetHashCode()); |
Post your comments here:
Name *:
Email : (optional)
» Your comments will be displayed only after manual approval.