Avatar of Jim Riddles
Jim RiddlesFlag for United States of America

asked on 

How to copy a List<string> in C# .NET5

I am attempting to copy a List<string> into a temporary variable to work on.  However, when I call RemoveAt on the temporary variable, it appears to be affecting the original variable.  How can I prevent this?
C#

Avatar of undefined
Last Comment
David H.H.Lee
Avatar of Bembi
Bembi
Flag of Germany image

It looks like you havn't set the variable in the right way...
You can assign a variable under windows by reference or by value.
Assigning by reference means, that the new variable is just a reference to the old one what means in fact, that changes to the variable also affects the source.
Assigning by value means, that the new variable takes over the content of the old one, so changed on the new variable do NOT reflect back to the old variable.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Peter Chan
Peter Chan
Flag of Hong Kong image

Hi,
If you want to copy the existing list, there is one Clone method in C# to duplicate it well. Read:
https://www.delftstack.com/howto/csharp/copy-a-list-in-csharp/

Avatar of Jim Riddles
Jim Riddles
Flag of United States of America image

ASKER

Thank you to all who participated.  I chose Ryan's answer since it displayed code to assist me in solving my issue.  I wanted to know if anybody had any insight as to which method is best practice.
Hi Jim,
You can always self-test which code performs better that best suit your actual case :)
Note: Change case 1 or case 2 based on your actual scenario.

using System.Diagnostics.Stopwatch

Open in new window

        Stopwatch stopWatch = new Stopwatch();
        TimeSpan ts;
        
      //case 1
      stopWatch.Start();
     
      //put your code - first senario here...
      
      stopWatch.Stop();
        
      // Get the elapsed time as a TimeSpan value.
        ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);
      
      //case 2
      stopWatch.Start();

     //put your code here - second senario here...

      stopWatch.Stop();
        
      // Get the elapsed time as a TimeSpan value.
        ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);


Open in new window


C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo