Q:

Write a C# Sharp program to creates two string objects with different values

0

Write a C# Sharp program to creates two string objects with different values. When it calls the Copy method to assign the first value to the second string, the output indicates that the strings represent different object references although their values are now equal. On the other hand, when the first string is assigned to the second string, the two strings have identical values because they represent the same object reference. 

Expected Output :

s1 = 'JAVA'                                                                      
s2 = 'Python''

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
class Example39
{
   public static void Main() 
   {
      string s1 = "JAVA";
      string s2 = "Python";
      Console.WriteLine("s1 = '{0}'", s1);
      Console.WriteLine("s2 = '{0}'", s2);
      Console.WriteLine("\nAfter String.Copy...");
      s2 = String.Copy(s1);
      Console.WriteLine("s1 = '{0}'", s1);
      Console.WriteLine("s2 = '{0}'", s2);
      Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(s1, s2));
      Console.WriteLine("Equals: {0}", Object.Equals(s1, s2));
      Console.WriteLine("\nAfter Assignment...");
      s2 = s1;
      Console.WriteLine("s1 = '{0}'", s1);
      Console.WriteLine("s2 = '{0}'", s2);
      Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(s1, s2));
      Console.WriteLine("Equals: {0}", Object.Equals(s1, s2));
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now