- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Cloning in C#
Cloning in C# is useful if you want to clone an array. The Clone() method in C# is used to create a similar copy of the array. C# has the Clone method and ICloneable interface.
Let us see an example to clone an array using the Clone() method −
Example
using System; class Program { static void Main() { string[] arr = { "one", "two", "three", "four", "five" }; string[] arrCloned = arr.Clone() as string[]; Console.WriteLine(string.Join(",", arr)); Console.WriteLine(string.Join(",", arrCloned)); Console.WriteLine(); } }
Output
one,two,three,four,five one,two,three,four,five
Above, we have a string array −
string[] arr = { "one", "two", "three", "four", "five" };
With that, in a new string array, we have used the Clone() method with the "as" operator to copy the array −
string[] arrCloned = arr.Clone() as string[];
Advertisements