2

Is it possible to somehow use this class(test.Value is not what i'm looking for):

RefBool test = false;
if (test)
{

}

This is class body:

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

    public static implicit operator RefBool(bool val)
    {
        return new RefBool(val);
    }

}
Iluvatar
  • 642
  • 7
  • 19
  • Is there a reason you can't just use `ref`? – David L Jul 20 '16 at 21:09
  • 2
    You can pass a boolean by reference by using the `ref` keyword in a method signature `private void WantBoolRef(ref bool someBool)` – Eric J. Jul 20 '16 at 21:09
  • I'm curious. Why would you want to have a 'ref bool'? Just to be able to modify it in some method call? – Trauer Jul 20 '16 at 21:10
  • 3
    Don't you (also) want `public static implicit operator bool(RefBool val) { return val.Value; }` ? – Heretic Monkey Jul 20 '16 at 21:11
  • 1
    https://msdn.microsoft.com/en-us/library/6x6y6z4d.aspx – Hans Passant Jul 20 '16 at 21:12
  • This looks like a typical A / B problem. Why do you need this ? Please illustrate an example of why this could be useful. Unless it's curiosity question, which in this case should be specified above. – Victor Zakharov Jul 20 '16 at 21:13
  • 1. In C# when you (ref bool) it is still pass by value 2. How can I vote for useful comment ? – Iluvatar Jul 20 '16 at 21:14
  • 3
    @Iluvatar no, `ref bool` is a value-type passed by-reference; it is not passed by-value. – Marc Gravell Jul 20 '16 at 21:15
  • For completeness: conversely, `RefBool` (without a `ref`) is a reference-type passed by-value – Marc Gravell Jul 20 '16 at 21:16
  • 1
    In my opinion, `MutableBool` is a better name for the `class` in this question. You can inject it in a constructor and keep it in a field, then you can always check to see if the value has mutated (flipped to the opposite value). Stuff like that you cannot do in the same way with `ref bool`. – Jeppe Stig Nielsen Jul 20 '16 at 21:28

1 Answers1

4

Yes, if you overload the true and false operators:

// note: you might want to think about what `null` means in terms of true/false
public static bool operator true(RefBool val) => val.Value;
public static bool operator false(RefBool val) => !val.Value;

I'm not sure it is a good idea, though; ref bool seems more obvious.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • It is quite silly that C# requires you to overload `operator false` in this case. The only operator you actually use is `operator true`. Unless we also overload `operator &`, the `operator false` overload is impossible to call. However, overloading `operator !` might be an idea. – Jeppe Stig Nielsen Jul 20 '16 at 21:23
  • @JeppeStigNielsen That's because these overloads are actually used to drive short-circuiting behavior of `||` and `&&` in conjunction with overloading the `|` and `&` operators. See [this answer](http://stackoverflow.com/a/33391/2779530) – Kyle Jul 20 '16 at 21:37
  • @Kyle I know `operator false` can be used when `&&` is used to call a user-defined `operator &`, but as I said, we do not have `operator &` here. So it ought to be legal to omit the `operator false` in this case. Our `operator true` has another use than short-circuiting. – Jeppe Stig Nielsen Jul 20 '16 at 21:43