using System;
namespace CopyingArray
{
class MainApp
{
static void CopyArray<T>(T[] source, T[] target)
{
for (int i = 0; i < source.Length; i++)
{
target[i] = source[i];
}
}
static void Main(string[] args)
{
string a = "hello";
string b = "GoodBye";
int[] source = { 1, 2, 3, 4 };
int[] target = new int[source.Length];
CopyArray<int>(source, target);
foreach (int element in target)
Console.WriteLine(element);
}
}
}
The arrangement is also a reference format, but why did it become a deep copy without using ref or out?
c#
First of all, you should understand the concepts of shallow copy and deep copy.
static void CopyArray<T>(T[] source, T[] target)
{
for (int i = 0; i < source.Length; i++)
{
target[i] = source[i];
}
}
In the code you posted, CopyArray method is an implementation of deep copy from source to target.
Light copy occurs when implemented as follows:
int[] source = { 1, 2, 3, 4 };
int[] target = source;
Java Data Type
Pr Primitive Type
B Boolean Type (boolean)
N Numeric Type
b Integral Type
b Integer Type (short, int, long)
b Floating Point Type (float, double)
Ch Character Type (char)
Re Reference Type
Cl Class Type
b Interface Type
(b) Array Type
En Enum Type
(b) Etc.
Since array type is a reference type, it works as call by reference.
579 Understanding How to Configure Google API Key
635 Uncaught (inpromise) Error on Electron: An object could not be cloned
624 GDB gets version error when attempting to debug with the Presense SDK (IDE)
575 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
930 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.