简体   繁体   English

如果用||怎么写呢? ,&&较短?

[英]How can I write this if with || , && shorter?

what if I would have something like this: 如果我会有这样的事情怎么办:

if (data == null || (data != null && (data.Count() != 3 || data.IsNotCorrect()))
{
  //error...
}

The data == null and then || 数据== null,然后|| (data != null) part is somehow ugly. (数据!= null)部分很难看。 Is that how to solve this problem in c#? 那是如何在C#中解决这个问题?

Edit: Sorry! 编辑:对不起! Changed data.IsCorrect() to data.IsNotCorrect() data.IsCorrect()更改为data.IsNotCorrect()

Well. 好。 data.Count() != 3 || data.IsCorrect() data.Count() != 3 || data.IsCorrect() . data.Count() != 3 || data.IsCorrect() What you are saying is that the data isn't correct if the count is 3. So why the extra if condition instead of doing that check internally? 您要说的是,如果计数为3,则数据不正确。那么为什么要使用额外的if条件而不是在内部进行检查呢?

if (data == null || data.IsCorrect())
{
  //error...
}

Update 更新资料

Seems to be confusion about what I mean with moving the validating logic to inside the class. 对于将验证逻辑移到类内部的含义,我似乎感到困惑。 All classes should be responsible of determine what's a valid state or not in proper OOP. 所有类都应负责确定什么是有效状态或未在适当的OOP中进行。 It's called encapsulation. 这就是所谓的封装。

This whole idea of letting the caller instead of the class itself determine what's correct or not have emerged more and more since the introduction of OR/Ms, mappers etc etc. But the fact is that classes which doesn't validate their own information is not really properly designed OOP classes. 自从引入OR / M,映射器等以来,让调用者(而不是类本身)确定正确或不正确的想法越来越多。但是事实是,不能验证自己信息的类不是真正设计合理的OOP类。 They are just containers like DTOs. 它们只是DTO之类的容器。

The danger with that is that each and every block of calling code is responsible of making sure that the DTO contains correct and valid information. 这样做的危险是,每个调用代码块都有责任确保DTO包含正确和有效的信息。 That means that there are n places in your code where a bug can be introduced instead of just 1 . 这意味着您的代码中有n地方可以引入错误,而不仅仅是1

So that's why I recommend that you move all validation logic to inside the IsCorrect or whatever you like to call it. 因此,这就是为什么我建议您将所有验证逻辑移至IsCorrect或任何您喜欢的名称中的原因。 But if you really want to code according to the basic OOP principles you should not let the class to get in an inconsistent state ever. 但是,如果您真的想根据基本的OOP原则进行编码,则不应让类陷入任何不一致的状态。 And that's done as I describe in the blog post below. 就像我在下面的博客文章中所描述的那样。

http://blog.gauffin.org/2012/06/protect-your-data/ http://blog.gauffin.org/2012/06/protect-your-data/

You can just leave off the (data != null && ... ) (ie leave the ... part). 您可以只保留(data != null && ... ) (即离开...部分)。

If the argument before || 如果||之前的参数 evaluates to true (ie data is null), the argument after || ||之后的参数为true (即data为空) is not evaluated - so you don't have to worry about null reference exceptions. 不会被评估-因此您不必担心null引用异常。 This is called short-circuiting . 这称为短路

You can remove the second data != null check because of lazy evaluation, and then you no longer need parens because you are left with only || 由于懒惰的评估,您可以删除第二个data != null检查,然后不再需要parens,因为只剩下|| operators: 运营商:

if (data == null || data.Count() != 3 || data.IsCorrect())

Of course the above is slightly suspect (I would have expected to see !data.IsCorrect() ). 当然,上面的内容有点!data.IsCorrect()怀疑(我本来希望看到!data.IsCorrect() )。

The right side of || ||的右侧 gets only evaluated if the left side isn't true ( || is short-circuiting) 仅在左侧不正确时评估( ||短路)

The conditional-OR operator ( || ) performs a logical-OR of its bool operands. 条件或运算符( || )对它的布尔操作数执行逻辑或。 If the first operand evaluates to true, the second operand isn't evaluated. 如果第一个操作数的值为true,则不计算第二个操作数。 If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false. 如果第一个操作数的计算结果为false,则第二个运算符将确定OR表达式作为一个整体的计算结果为true还是false。

Quoted from || 引用|| Operator (C# Reference) (MSDN) 运算符(C#参考) (MSDN)

Apply that to your expression: 将其应用于您的表情:

data == null || (data != null && (data.Count() != 3 || data.IsCorrect())

This means when data != null is reached, data is guaranteed to be different from null , and thus data != null is guaranteed to be true, and you can leave it out. 这意味着,当到达data != null时,保证datanull不同,因此,保证data != null为真,您可以将其省略。

Thus your expression is equivalent to: 因此,您的表达式等同于:

data == null || data.Count() != 3 || data.IsCorrect()

Just get rid of the data != null part. 只是摆脱data != null部分。 Since you already gone passed the first part of the statement, you know by that time that data is not null, so no need to check it. 由于您已经通过了语句的第一部分,因此到那时您就知道数据不为空,因此无需检查它。

if (data == null || (data.Count() != 3 || data.IsCorrect()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM