In C#, Copy() is a string method. It is used to create a new instance of String with the same value for a specified String. The Copy() method returns a String object, which is the same as the original string but represents a different object reference. To check its reference, use assignment operation, which assigns an existing string reference to an additional object variable.
Syntax:
public static string Copy(string str)
Explanation : This method accepts single parameter str which is the original string to be copied. And it returns the string value, which is the new string with the same value as str. The type of Copy() method is System.String.
Example Program to illustrate the Copy() Method
// C# program to demonstrate the // use of Copy() method using System; class Program { static void cpymethod() { string str1 = "GeeksforGeeks"; string str2 = "GFG"; Console.WriteLine("Original Strings are str1 = " + "'{0}' and str2='{1}'", str1, str2); Console.WriteLine(""); Console.WriteLine("After Copy method"); Console.WriteLine(""); // using the Copy method // to copy the value of str1 // into str2 str2 = String.Copy(str1); Console.WriteLine("Strings are str1 = " +"'{0}' and str2='{1}'", str1, str2); // check the objects reference equal or not Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(str1, str2)); // check the objects are equal or not Console.WriteLine("Equals: {0}", Object.Equals(str1, str2)); Console.WriteLine(""); Console.WriteLine("After Assignment"); Console.WriteLine(""); // to str1 object reference assign to str2 str2 = str1; Console.WriteLine("Strings are str1 = '{0}' " +"and str2 = '{1}'", str1, str2); // check the objects reference equal or not Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(str1, str2)); // check the objects are equal or not Console.WriteLine("Equals: {0}", Object.Equals(str1, str2)); } // Main Method public static void Main() { // calling method cpymethod(); } } |
Original Strings are str1 = 'GeeksforGeeks' and str2='GFG' After Copy method Strings are str1 = 'GeeksforGeeks' and str2='GeeksforGeeks' ReferenceEquals: False Equals: True After Assignment Strings are str1 = 'GeeksforGeeks' and str2 = 'GeeksforGeeks' ReferenceEquals: True Equals: True
Reference : https://msdn.microsoft.com/en-us/library/system.string.copy
Recommended Posts:
- Shallow Copy and Deep Copy in C#
- File.Copy(String, String) Method in C# with Examples
- File.Copy(String, String, Boolean) Method in C# with Examples
- C# | Copy StringCollection at the specified index of array
- C# | Copy the Stack to an Array
- C# | Copy the elements of a string array to the end of the StringCollection
- C# | Copy StringDictionary to Array at the specified index
- C# | Copy ListDictionary to Array instance at the specified index
- C# | Copy Constructor
- C# | Get a read-only copy of the OrderedDictionary
- C# | Copy the entire LinkedList<T> to Array
- C# | How to copy a String into another String
- C# | Copy the elements of collection over a range of elements in ArrayList
- C# | Copy OrderedDictionary elements to Array instance at the specified index
- How to create a shallow copy of SortedList Object in C#
- C# | How to copy the entire ArrayList to a one-dimensional Array
- How to create a shallow copy of Hashtable in C#
- How to create a shallow copy of ArrayList in C#
- How to create a shallow copy of BitArray in C#
- C# | How to create a shallow copy of the BitArray
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

