Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#覆盖另一个函数中的字符串和字符串[]_C#_Arrays_String_Function - Fatal编程技术网

C#覆盖另一个函数中的字符串和字符串[]

C#覆盖另一个函数中的字符串和字符串[],c#,arrays,string,function,C#,Arrays,String,Function,我覆盖setValues()函数中的变量“arr[1]”和“test” arr[1]已更改为“BBB” 但测试不会变为“222” 输出:BBB111 但它应该是BBB222 为什么字符串测试没有得到更新 public class Program { static void Main(string[] args) { string[] arr = new string[10]; arr[1] = "AAA";

我覆盖setValues()函数中的变量“arr[1]”和“test”

arr[1]已更改为“BBB”

但测试不会变为“222”

输出:BBB111

但它应该是BBB222

为什么字符串测试没有得到更新

public class Program
    {
        static void Main(string[] args)
        {
            string[] arr = new string[10];

            arr[1] = "AAA";
            string test = "111";

            setValues(arr, test);

            int exit = -1;
            while (exit < 0)
            {

                for (int i = 0; i < arr.Length; i++)
                {
                    if (!String.IsNullOrEmpty(arr[i]))
                    {
                        Console.WriteLine(arr[i] + test);
                    }
                }
            }
        }

        private static void setValues(string[] arr, string test)
        {
            arr[1] = "BBB";
            test = "222";
        }
    }
公共类程序
{
静态void Main(字符串[]参数)
{
字符串[]arr=新字符串[10];
arr[1]=“AAA”;
字符串测试=“111”;
设定值(arr,测试);
int exit=-1;
while(退出<0)
{
对于(int i=0;i
这是因为
setValues
方法中的
test
prameter没有标记为
ref
,因此它只在方法内部更改,而值不会在外部更改。

您只是在
setValues
函数中将本地引用更改为
test
。您需要通过引用传递此变量(
ref

那么就这样称呼它:

setValues(arr, ref test);

您需要通过引用传递该字符串才能在方法中修改它,您可以通过添加
ref
关键字:

public class Program
    {
        static void Main(string[] args)
        {
            string[] arr = new string[10];

            arr[1] = "AAA";
            string test = "111";

            setValues(arr, ref test);

            int exit = -1;
            while (exit < 0)
            {

                for (int i = 0; i < arr.Length; i++)
                {
                    if (!String.IsNullOrEmpty(arr[i]))
                    {
                        Console.WriteLine(arr[i] + test);
                    }
                }
            }
        }

        private static void setValues(string[] arr, ref string test)
        {
            arr[1] = "BBB";
            test = "222";
        }
    }
公共类程序
{
静态void Main(字符串[]参数)
{
字符串[]arr=新字符串[10];
arr[1]=“AAA”;
字符串测试=“111”;
设定值(arr、ref测试);
int exit=-1;
while(退出<0)
{
对于(int i=0;i
因为数组作为对象是通过引用传递的,而字符串是通过值传递的。 因此,函数setValues()更新test的本地副本,该副本对调用方不可见,同时更新字符串[]arr的唯一实例,该实例对调用方可见,而对被调用方可见


保罗

原因是范围可变。SetValues()方法中引用的变量与Main中引用的变量不同。 根据课程的需要,有两种解决方法:

  • 如其他答案所述,在SetValues方法中通过引用传递值(这将保留代码的原样):
  • 将变量声明移到Main主体之外(这会将变量的作用域更改为类级变量,从而将它们的作用域更改为整个类,并可能允许从其他类访问它们):
  • 然后也通过引用调用它:

    设定值(arr、ref测试)


    此外,为了补充下面的答案,字符串对象是不可变的:它们在创建后不能更改。所有用于修改字符串的字符串方法和C#运算符实际上会在新的字符串对象中返回结果。我建议您阅读Jon Skeet的文章。您还需要使用
    ref
    调用该方法。为什么字符串测试需要该“ref”,而String[]arr不需要?@Chelayos因为使用String[]您正在将引用传递给数组,但未修改该数组。您要修改的是数组的内容,而不是指向数组本身的指针。您可以通过在setValues中设置arr=new string[0]来测试这一点,并且此更改不会传播给调用方。这两个值都作为值传递。具体来说,传递的值是对对象的引用。请注意,
    arr=newstring[0]不会更新传递到方法中的变量。区别在于数组的内容被更新了,而字符串引用的副本被更新了。@juharr:我的意思是,正如您发布的链接[…]中所述,类类型、接口类型、委托类型和数组类型都是引用类型。[...]. 所以,如果我理解的话,数组作为引用传递是因为它是引用类型。不,它仍然作为值传递,只是传递的值是引用。我发布的链接的区别在于数组是引用类型,但它作为值传递的参数类型。这里有一个直接的引语,我认为如果说对象是默认通过引用传递的,而不是说对象引用是默认通过值传递的,这是非常令人困惑的。即使变量的类型决定了它是如何传递的,那么你也必须认识到
    字符串
    是一个引用类型而不是值类型(事实上,它是不可变的,并且具有小写别名,这使得事实不那么明显)。在这种情况下,作用域不是问题所在,因为您要将变量传递给方法,它与不通过引用调用它有关。另外,您对
    arr
    test
    的声明是
    Program
    的实例变量,而
    Main
    static
    ,因此
    arr
    test
    不可用(没有能够使用它们的
    新程序)。正确。我本应该将我的示例颠倒过来,并进一步扩展,将它们移到声明之外,但这两种方法都是基于类的需要回答问题的有效方法。由于这是一个基本(但非常好)的问题,而且用户在网站上的声誉很低,我觉得有必要提供更多的信息
    
    public class Program
        {
            static void Main(string[] args)
            {
                string[] arr = new string[10];
    
                arr[1] = "AAA";
                string test = "111";
    
                setValues(arr, ref test);
    
                int exit = -1;
                while (exit < 0)
                {
    
                    for (int i = 0; i < arr.Length; i++)
                    {
                        if (!String.IsNullOrEmpty(arr[i]))
                        {
                            Console.WriteLine(arr[i] + test);
                        }
                    }
                }
            }
    
            private static void setValues(string[] arr, ref string test)
            {
                arr[1] = "BBB";
                test = "222";
            }
        }
    
    private static void setValues(string[] arr, ref string test)
    {
        arr[1] = "BBB";
        test = "222";
    }
    
    public class Program
        {
            static string[] arr = new string[10];
            static string test = "111";
    
            static void Main(string[] args)
            {
                arr[1] = "AAA";