18

我在这里的一堵砖墙上。是否可以将一个布尔值复制到另一个布尔值。考虑这段代码。. .

bool a = false;
bool b = a;

b 现在是一个完全独立的布尔值,值为 false。如果我随后更改 a,它将对 b 没有影响。是否可以通过 ref 使 a = b?我该怎么做?

非常感谢

4

8 回答 8

41

不,因为 bool 是一种值类型,所以它总是按值复制。

最好的选择是将您的 bool 包装在一个类中 - 这将为其提供引用类型语义:

public class BoolWrapper
{
     public bool Value { get; set; }
     public BoolWrapper (bool value) { this.Value = value; }
}

BoolWrapper a = new BoolWrapper(false);
BoolWrapper b = a;
b.Value = true; 
 // a.Value == true 
于 2009-09-16T19:00:39.943 回答
18

感谢@Reed 的回答(+1)!他鼓励我采用更“通用”的解决方案!:)

public class ValueWrapper<T> where T : struct
{
    public T Value { get; set; }
    public ValueWrapper(T value) { this.Value = value; }
}
于 2012-04-24T18:52:14.067 回答
14

安德烈答案的小扩展......这允许您最终直接将其分配给您想要的任何类型。所以:

        ValueWrapper<bool> wrappedBool = new ValueWrapper<bool>(true);

        bool unwrapped = wrappedBool;  // you can assign it direclty:

        if (wrappedBool) { // or use it how you'd use a bool directly
            // ...
        }

public class ValueWrapper<T>
{

    public T Value { get; set; }

    public ValueWrapper() { }

    public ValueWrapper(T value) { 
        this.Value = value; 
    }

    public static implicit operator T(ValueWrapper<T> wrapper)
    {
        if (wrapper == null) {
            return default(T);
        }
        return wrapper.Value;
    }

}

于 2015-03-24T23:48:28.947 回答
6

这可能不是您想要的,但是如果您的场景是您想要一个调用来修改本地布尔值的函数,您可以使用 ref 或 out 键。

bool a = false;

F(ref a);
// a now equals true

...

void F(ref bool x)
{
x = true;
}
于 2009-09-16T19:07:41.493 回答
0

bool 是一种值类型,不能通过引用复制。

于 2009-09-16T18:59:59.713 回答
0

所以我猜你需要传递一个布尔引用,你不能用'BoolWrapper'类包装,因为布尔存在一些你不能或不想修改的地方。

可以办到!

首先声明任何布尔引用的样子

/// <summary> A reference to a bool.</summary>
/// <param name="value">new value</param>
/// <returns>Value of boolean</returns>
public delegate bool BoolRef(bool? value = null);

现在您可以像这样引用 myBool

    bool myBool; // A given bool that you cannot wrap or change
    private bool myBoolRef(bool? value) {
        if (value != null) {
            myBool = (bool)value;
        }
        return myBool;
    }

并像这样使用它:

    void myTestCaller() {
        foo(myBoolRef);
    }
    void foo(BoolRef b) {
        bool c = b(); // get myBool
        b(true); // set myBool to true
    }

同样的技巧适用于其他值类型,例如 int

于 2013-08-23T14:27:31.607 回答
0

我有一个案例,我希望一个班级更改另一个班级的布尔值 - 请注意,有更好的方法来处理这种情况,但这是使用 Actions 的概念证明。

public class Class1 
{
    bool myBool { get; set; }
    void changeBoolFunc(bool val) { myBool = val; }
    public Class1() 
    {
        Action<bool> changeBoolAction = changeBoolFunc;
        myBool = true; 
        Console.WriteLine(myBool);    // outputs "True"
        Class2 c2 = new Class2(changeBoolAction);
        Console.WriteLine(myBool);    // outputs "False"
    }
}
public class Class2
{
    public Class2(Action<bool> boolChanger) { boolChanger(false); }
}
void Main()
{
    Class1 c1 = new Class1();
}
于 2014-11-29T15:34:27.080 回答
-3

只需将标志用作Nullable<bool>orbool?并将其设置在传递给通用方法的结构中。上面的ValueWrapper<T>类基本上就是做什么Nullable<T>的。

于 2012-09-17T07:07:19.403 回答