空合并运算符和运算符 &&在 C# 中

发布于 2024-08-30 16:43:28 字数 455 浏览 4 评论 0原文

是否可以在下一种情况下一起使用任何方式运算符 ?? 和运算符 &&

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

这意味着下一步:

  • 如果 any 为 null然后 this.Any.HasValue 返回 false
  • 如果 any 有值,那么它会返回考虑另一个布尔属性的值,即 Any & &一些布尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

毁我热情 2024-09-06 16:43:28

我想知道为什么到目前为止没有人建议这样做:

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

都会返回

  • 如果 any 为 null,则无论 this.SomeBool 的值是什么, null是;
  • true 如果 anythis.SomeBool 都为 true; 。
  • 如果 any 不为 null,且 this.SomeBool 为 false,则为 false

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.
青衫负雪 2024-09-06 16:43:28

由于您希望在源为 null 的情况下返回 null,因此我认为 ?? 不会帮助您将其写得更短或者更清楚。

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.

过期情话 2024-09-06 16:43:28

你是这个意思吗?

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

我将返回值保留为布尔值?但看起来它可以改为 bool。

这是这样测试的:

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;
        }
    }
}

它返回

False
False
True
False
False
False

这里的行为与原始行为不太一样,因为应该返回 null 以使测试用例 1 和 4 相同。但也许这种行为不是必需的?

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?

舂唻埖巳落 2024-09-06 16:43:28

Null Coalescing 运算符不适用于您构建方法逻辑的方式。当然你可以把它强行放在那里,但它看起来会很难看,而且只会让读它的人感到困惑。

我发现原始代码很难阅读和理解,因此重构并删除了三元运算符以揭示意图。

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

if (any == null)
    return null;

return any.Value && this.SomeBool;

空合并只是很好的速记,应该明智地使用它

Person contact = Mother ?? Father ?? FirstSibling;

比以下内容更能揭示意图,并且更容易阅读和维护:

Person contact = Mother;
if (contact == null)
    contact = Father;
if (contact == null)
    contact = FirstSibling;

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;
杀手六號 2024-09-06 16:43:28

我认为你想要做的是这样的:

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

但是,我认为在这种情况下,使用 if 块可能更清楚:

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

如果有任何是 null,那么你想返回 truenull,对吗?

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?

随风而去 2024-09-06 16:43:28

问题是,你真的不想使用 ??操作员。它的目的是让您可以轻松避免空值,实际上您想保留空值。

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.

往日 2024-09-06 16:43:28

这具有您所描述的行为:

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

This has the behavior you describe:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文