Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7611957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:47:37+00:00 2026-05-31T01:47:37+00:00

I am attempting to copy a number of arrays in c# using the Array.Copy()

  • 0

I am attempting to copy a number of arrays in c# using the Array.Copy() method. This method is supposed to automatically type-convert, however I am wondering if it will convert user-defined types if I were to define a constructor like so:

OldType(){
int param1;
String param2;
}

NewType(OldType old){
setParam1(old.param1);
setParam2(old.param2);
}

OldType oldArray[];
NewType newArray[];

//will this automatically convert???
Array.Copy(oldArray, newArray);

This is for use in converting from a legacy object to a new object. The content is exactly the same, the names are just different.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:47:38+00:00Added an answer on May 31, 2026 at 1:47 am

    There’s an easy way to make this conversion without leaving yourself or anybody reading your code in doubt: use LINQ.

    OldType[] oldArray = /* Some logic */;
    NewType[] newArray = oldArray.Select(old => new NewType(old)).ToArray();
    

    As the documentation here (pointed out by M.Babcock in the comments) says, Array.Copy is an O(n) operation. Likewise, the LINQ code I wrote above is an O(n) operation, so there is no asymptotic performance hit (actual performance in wall clock time is dependent on a host of factors, so I can’t compare the runtime of the two solutions without profiling) and you know that this code is correct.

    Also from the MSDN docs:

    If sourceArray and destinationArray are both reference-type arrays or are both arrays of type Object, a shallow copy is performed. A shallow copy of an Array is a new Array containing references to the same elements as the original Array. The elements themselves or anything referenced by the elements are not copied. In contrast, a deep copy of an Array copies the elements and everything directly or indirectly referenced by the elements.

    So no, Array.Copy won’t “automatically convert” i.e. actually create new objects of type NewType during the copy, but will simply copy references to the objects of type OldType and put those references in newArray.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to copy the database located, as an example, in: /data/data/com.x.x.x/databases/mydb.db This works
I am attempting to mirror a directory on a remote server using rsync. However,
I'm attempting to compile a working copy of the MagickNet class library (DLL) using
I'm attempting a simple test of binary file I/O using the STL copy algorithm
I'm attempting to set up a windows-based FTP server using Apache FTPServer, however I
I am attempting to copy an array in my ViewController to a Data storage
Audio Messaging Interchange Specification. I am attempting to locate a copy of this rather
I am attempting to insert a copy of a row from one table into
I'm attempting to use the adb pull command to copy only certain files (jpg)
Attempting to use the data series from this example no longer passes the JSONLint

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.