3

Is it possible to use together any way operator ?? and operator && in next case:

bool? Any
{
   get
   {
      var any = this.ViewState["any"] as bool?;
      return any.HasValue ? any.Value && this.SomeBool : any;
   }
}

This means next:

  • if any is null then this.Any.HasValue return false
  • if any has value, then it returns value considering another boolean property, i.e. Any && SomeBool
abatishchev
  • 98,240
  • 88
  • 296
  • 433

7 Answers7

4

Since you want to return null in case the source is null, I don't think ?? is going to help you write this any shorter or clearer.

H H
  • 263,252
  • 30
  • 330
  • 514
  • It seems that you're right. Operator `??` is for returning a value in any case. This is exactly what I don't need.. – abatishchev Apr 27 '10 at 22:35
4

I'm wondering why nobody has suggested this so far:

bool? any = this.ViewState["any"] as bool?;
return any & this.SomeBool;

This returns

  • null if any is null, no matter what the value of this.SomeBool is;
  • true if both any and this.SomeBool are true; and
  • false if any is not null, and this.SomeBool is false.
abatishchev
  • 98,240
  • 88
  • 296
  • 433
dtb
  • 213,145
  • 36
  • 401
  • 431
  • code looks good, description is wrong though (how can you return both `null` and `false` at the same time, yet that's what your rules say would happen for `any == null` and `this.SomeBool == false`. – Ben Voigt Apr 28 '10 at 00:32
  • @Henk Holterman: Why are the operands `'bool' and 'bool?'` and not `'bool?' and 'bool'`!? The left operand is clearly of type `bool?` as I declared it as such. – dtb Apr 28 '10 at 10:16
  • 1
    @Henk Holterman: Yes, but I made a typo when copying it to my answer :-( Fixed it now. My test program: `bool? x = null; bool y = true; bool? z = x & y;` – dtb Apr 28 '10 at 11:18
  • Your code can be rewritten as: `return this.ViewState["any"] as bool? & this.SomeBool;`, right? – abatishchev Apr 28 '10 at 16:47
  • @abatishchev: I think so. May need some `(`/`)`. – dtb Apr 28 '10 at 16:53
  • 3
    An important caveat: `&&` is a short-circuiting boolean evaluation, but the bitwise evaluator `&` **doesn't** short-circuit. Therefore, in the above, this.SomeBool will be evaluated regardless. So be mindful that this is not a complete replacement for the typical usage of `&&` when using `Nullable`. – Robert Paulson Apr 28 '10 at 23:49
3

Is this what you mean?

bool? Any 
{ 
   get 
   { 
      return ((this.ViewState["any"] as bool?) ?? false) && this.SomeBool;
   } 
} 

I've left the return value as bool? but it looks like it could be changed to just bool.

This was tested like this:

class Program
{
    private static readonly Dictionary<string, object> ViewState = new Dictionary<string, object>();
    private static bool SomeBool;

    static void Main(string[] args)
    {
        ViewState["any"] = (bool?)null; SomeBool = true; Console.WriteLine(Any);
        ViewState["any"] = (bool?)false; SomeBool = true; Console.WriteLine(Any);
        ViewState["any"] = (bool?)true; SomeBool = true; Console.WriteLine(Any);
        ViewState["any"] = (bool?)null; SomeBool = false; Console.WriteLine(Any);
        ViewState["any"] = (bool?)false; SomeBool = false; Console.WriteLine(Any);
        ViewState["any"] = (bool?)true; SomeBool = false; Console.WriteLine(Any);
        Console.ReadLine();
    }

    static bool? Any
    {
        get
        {
            return ((ViewState["any"] as bool?) ?? false) && SomeBool;
        }
    }
}

which returns

False
False
True
False
False
False

The behaviour here is not quite the same as the original as null should be returned for test cases 1 and 4 to be identical. But maybe that behaviour isn't required?

Daniel Renshaw
  • 33,729
  • 8
  • 75
  • 94
  • Unfortunately, no. `result as bool?` has always `HasValue = true`. doesn't it? – abatishchev Apr 27 '10 at 22:25
  • 1
    He wants to return null if any is null AND the two other conditions are false. So, he wants TRUE, FALSE and NULL – Armstrongest Apr 27 '10 at 22:34
  • Daniel, thank you very much for your detailed answer! But unfortunately we talk about different behaviors - I need to keep return value being `Nullable` so checking to `HasValue` would have a sense – abatishchev Apr 27 '10 at 22:37
  • I understand and in that case I agree with Henk that there isn't any way to use ?? and && in a way that makes it shorter/clearer whilst maintaining that behaviour. – Daniel Renshaw Apr 27 '10 at 22:40
3

The Null Coalescing operator isn't going to work for how you've structured the logic for your method. Sure you could force it in there, but it's going to look ugly and just confuse whomever reads it.

I found the original code hard to read and understand, so refactored and removed the ternary operator to reveal intentions.

bool? any = this.ViewState["any"] as bool?;

if (any == null)
    return null;

return any.Value && this.SomeBool;

Null coalescing is just nice shorthand and should be used judiciously

Person contact = Mother ?? Father ?? FirstSibling;

Is more intention revealing, and easier to read + maintain than:

Person contact = Mother;
if (contact == null)
    contact = Father;
if (contact == null)
    contact = FirstSibling;
Robert Paulson
  • 17,603
  • 5
  • 34
  • 53
2

I think what you're trying to do is this:

return any ?? (any.Value && this.SomeBool) ? true : new Nullable<bool>();

However, I think in cases like this, it's probably more clear to use an if block:

if ( !any.HasValue )
  return (any.Value && this.SomeBool) ? true : any;
else 
  return any;

If any is null, then you want to return true or null, right?

Armstrongest
  • 15,181
  • 13
  • 67
  • 106
1

thing is, you don't really want to use the ?? operator. Its meant to make it easy to avoid nulls, you actually want to keep nulls.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
-1

This has the behavior you describe:

return (any ?? false) && this.SomeBool

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Chris Pitman
  • 12,990
  • 3
  • 41
  • 56