C#引用数组 [英] C# Array of references

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

问题描述

我该怎么做?

int v1 = 4;
int v2 = 3;
int v3 = 2;
int v4 = 1;

int [] vars = new int [] {ref v1, ref v2, ref v3, ref v4};

for (var i = 0; i < 4; i++) {
    ChangeVar (vars [i], i);
}

void ChangeVar (ref int thatVar, int newValue) {
    thatVar = newValue;
}

我想这样做是因为其他类直接访问了这些变量.例如v1可以是某物的宽度,而v2可以是某物的高度.我的某些类使用width变量来限制必须从用户那里获取的输入的长度.一些类使用height变量来做其他事情.但是我希望能够使用循环来编辑这些变量,因为现在这是编辑过程的工作方式:

I want to do this because those variables are accessed directly by other classes. Such as v1 could be the width of something and v2 could be the height of something. Some of my classes use the width variable to limit the length of the input it has to get from the user. Some classes use the height variable to do something else. But I want to be able to edit those variables using a loop because right now this is how the edit process works:

int indexOfVarToChange = GetIndex ();

switch (indexOfVarToChange) {
    case 0:
        int newValue = GetNewValue ();
        width = newValue;
        break;
    case 1:
        int newValue = GetNewValue ();
        height = newValue;
        break;
}

我必须手动重新分配变量,因为我无法在循环中使用对这些变量的引用数组.我必须执行30多个唯一变量,这很痛苦.

I have to manually reassign the variables because I can't have an array of references to those variables to use in a loop. I have over 30 unique variables that I have to do this for and it's a pain.

我想后备计划是将所有这些变量移到Dictionary中,并拥有所有键的数组,并将每个键传递给编辑功能.

I guess the fallback plan would be to move all those variables into a Dictionary and have an array of all the keys and pass each key to the editing function.

推荐答案

不,你不能.

您仍然可以就地编辑元素,但只能直接分配给它们:

You can still edit the elements in place, but only by assigning directly into them:

 vars[2] += 42;

但是我刚刚测试了这个方法:

But I just tested this works:

using System;
public class Test
{
        private static void assign(ref int i)
        {
             i = 42;
        }

        public static void Main()
        {
              var vars = new [] { 1,2,3,4 };
              Console.WriteLine(vars[2]);
              assign(ref vars[2]);
              Console.WriteLine(vars[2]);
        }
}

查看 在线http://ideone.com/fz36y

See it LIVE http://ideone.com/fz36y

输出

3
42

更新:包装器

作为一种心理锻炼,我想出了这种生病缠结的机制,仍然可以得到想要的东西(但要比仅仅装箱所有整数要花更多的钱):

Update: Wrapper

As a mental exercise, I came up with this sick-and-twisted mechanism to still get what you want (but at even more cost than simply boxing all the ints):

private class Wrap<T> where T : struct
{
    public T Value;

    public static implicit operator Wrap<T>(T v) { return new Wrap<T> { Value = v }; }
    public static implicit operator T(Wrap<T> w) { return w.Value; }

    public override string ToString() { return Value.ToString(); }
    public override int GetHashCode() { return Value.GetHashCode(); }
    // TODO other delegating operators/overloads
}

现在,Wrap<int>的行为大致与常规int相同(在比较,相等和运算符领域需要做更多的工作).您可以使用它来编写此代码,并使它按您想要的方式工作:

Now, a Wrap<int> will behave roughly as a regular int (needs more work in the field of comparison, equality and operators). You can use it to write this, and have it work the way you wanted:

private static void assign(ref int i)
{
    i = 42;
}

public static void Main()
{
    Wrap<int> element = 7;
    var vars = new Wrap<int>[] {1, 2, element, 3, 4};
    Console.WriteLine(vars[2]);
    assign(ref vars[2].Value);
    Console.WriteLine(element);

    Console.ReadKey();
}

输出:

7
42

查看它 也可以在线观看:http://ideone.com/b0m7T

See it live too: http://ideone.com/b0m7T

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

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