shallow radiation deep radiation

Asked 2 years ago, Updated 2 years ago, 36 views

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#

2022-09-22 18:12

1 Answers

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.


2022-09-22 18:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.