
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# String - Clone() Method
The C# String Clone() method is used to shallow copy of the string object. However, It's important to note that the clone method does not create a new string. It simply creates another view of the same data.
A shallow copy is a new object that shares the same instance variable as an existing object.
Syntax
Following is the syntax of the C# string Clone() method −
public object Clone();
Parameters
This method does not accept any parameters −
Return value
This method returns instance of String.
Example 1
This is the basic example of the string Clone() method. We can use this method to create another view of the same data.
using System; class Program { static void Main() { string original_string = "Hello, TutorialsPoint !"; object cloned_string = original_string.Clone(); Console.WriteLine($"Original: {original_string}"); Console.WriteLine($"Cloned: {cloned_string}"); Console.WriteLine($"Are both references equal? {ReferenceEquals(original_string, cloned_string)}"); } }
Output
Following is the output −
Original: Hello, TutorialsPoint ! Cloned: Hello, TutorialsPoint ! Are both references equal? True
Example 2: Modify The String
Let us see another example of the Clone() method. Here, we modify the original string after cloning −
using System; class Program { static void Main() { string original_str = "Learning C# in tutorialspoint is fun"; object cloned_str = original_str.Clone(); Console.WriteLine("Original String: " + original_str); Console.WriteLine("Cloned String: " + cloned_str); // Modify the original string original_str = "I love tutorialspoint"; Console.WriteLine("\nAfter modifying the original string:"); Console.WriteLine("Original String: " + original_str); Console.WriteLine("Cloned String: " + cloned_str); } }
Output
Following is the output −
Original String: Learning C# in tutorialspoint is fun Cloned String: Learning C# in tutorialspoint is fun After modifying the original string: Original String: I love tutorialspoint Cloned String: Learning C# in tutorialspoint is fun
Example 3: Modify The Cloned String
Here, in this example we modified the cloned string −
using System; class Program { static void Main() { string original = "Hii, TutorialsPoint!"; object cloned = original.Clone(); Console.WriteLine("Original String: " + original); Console.WriteLine("Cloned String: " + cloned); string modified_cloned = ((string)cloned).ToUpper(); Console.WriteLine("\nAre the references equal? " + ReferenceEquals(original, cloned)); Console.WriteLine("\nAfter modifying the cloned string:"); Console.WriteLine("Modified Cloned" + modified_cloned); } }
Output
Following is the output −
Original String: Hii, TutorialsPoint! Cloned String: Hii, TutorialsPoint! Are the references equal? True After modifying the cloned string: Modified ClonedHII, TUTORIALSPOINT!
csharp_strings.htm
Advertisements