C#变量范围 [英] C# variable scope

查看:86
本文介绍了C#变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下示例:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;

namespace ArrayTest
{
    [TestClass]
    public class UnitTest1
    {
        class TestClass
        {
            public byte[] arr = null;
        }

        void F2(TestClass c)
        {
            byte[] arr2 = new byte[] { 0x1, 0x2 };
            c.arr = arr2;
        }

        void F1(byte[] arr)
        {
            byte[] arr2 = new byte[] { 0x1, 0x2 };
            arr = arr2;
        }

        [TestMethod()]
        public void ArrayTest()
        {
            byte[] arr1 = null;

            // Example 1 - array passed as a parameter
            F1(arr1);

            if (arr1 == null)
                Debug.WriteLine("Standalone array is null");
            else
                Debug.WriteLine($"Standalone array size: {arr1.Length}");
            // Example 1 output: "Standalone array is null"

            // Example 2 - array in the class is passed as a parameter
            TestClass c2 = new TestClass();
            F1(c2.arr);
            if (c2.arr == null)
                Debug.WriteLine("\nArray in the class c2 is null");
            else
                Debug.WriteLine($"\nArray size in the class c2: {c2.arr.Length}");
            // Example 2 output: "Array in the class c2 is null"

            // Example 3 - class is passed as a parameter
            TestClass c3 = new TestClass();
            F2(c3);

            if (c3.arr == null)
                Debug.WriteLine("\nArray in the class c3 is null");
            else
                Debug.WriteLine($"\nArray size in the class c3: {c3.arr.Length}");
            // Example 3 output: Array size in the class c3: 2

        }

    }
}




为什么示例3中的输出与示例1和示例2不同?我希望它是相同的,但事实并非如此。


Why is the output in Example 3 is different from Example 1 and Example 2? I would expect it to be the same but it isn't.

推荐答案

我认为这是因为在F1中byte []作为值传递,这是默认值所有价值观。因此,调用者看不到对byte []的更改。 

I think it's because in F1 the byte[] is passed as value, which is the default for all values. So, changes to the byte[] are not visible for the caller. 

要将其作为参考传递,您需要使用ref或out关键字。您将看到您将得到不同的结果。

To pass it as reference you need to do it by using the ref or out keyword. You will see that you'll get a different result.

这是一个简单的测试,可视化您的原始方法不会影响初始字节[]:

Here is a simple test to visualize that your oriiginal method doesn't affect the initial byte[]:

static void F1(byte[] arr, TestClass c)
{
    byte[] arr2 = new byte[] { 0x1, 0x2 };
    arr = arr2;

    if (arr == c.arr)
    {
	Debug.WriteLine("Both variables reference the same object on the heap.");
    }
    else
    {
	Debug.WriteLine("Each variable references its own object.");
    }
}

TestClass c2 = new TestClass();
F1(c2.arr, c2);

如果你现在使用ref关键字

If you now use the ref keyword

static void F1(ref byte[] arr, TestClass c)
{
    byte[] arr2 = new byte[] { 0x1, 0x2 };
    arr = arr2;

    if (arr == c.arr)
    {
	Console.WriteLine("Both variables reference the same object on the heap.");
    }
    else
    {
	Console.WriteLine("Each variable references its own object.");
    }
}

TestClass c2 = new TestClass();
F1(ref c2.arr, c2);

您将看到两个变量都将引用同一个对象。有关详情,请向Jon Skeet推荐
这篇文章

you will see that both variables will reference the same object. For more details I recommend this article by Jon Skeet.

wizend


这篇关于C#变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆