简体   繁体   English

JavaScript 中的逻辑运算符 && 和它的 C# 等效

[英]Logical operator && in JavaScript and it's C# equivalent

In JavaScritp operator && can be used to invoke functions like this在 JavaScritp 中,运算符 && 可用于调用这样的函数

possibleNull && possibleNull.do()

This executes do() method of the possibleNull object only of possibleNull object is not null.这仅执行 possibleNull object 不是 null 的 possibleNull object 的 do() 方法。 This can be handled by?.这个可以处理吗? operator.操作员。 However I coud not find C# alternative for但是我找不到 C# 替代品

let foo = _fooProvider.GetFoo();
return foo && processFoo(foo);

In "left && right" epxression in JS, when left variable be evaluated to "true" (generaly not null -> there are more cases in JS though), right variable is returned (or executed).在 JS 的 "left && right" 表达式中,当左侧变量被评估为 "true" 时(一般不是 null -> JS 中有更多情况),返回(或执行)右侧变量。

I know about C# alternative for ||我知道 C# 替代 || JS operator that works almost the same (?? operator).工作原理几乎相同的 JS 运算符(?? 运算符)。 Is there also a counterpart for &&? &&也有对应的吗?

Edit: I know that && can be short-circuited when both operands are bool.编辑:我知道当两个操作数都是布尔值时 && 可以短路。 However in JS you can have right side that returns something else than bool.但是,在 JS 中,您可以让右侧返回 bool 以外的其他内容。 Left side in JS is indeed automaticaly converted to bool by the interpreter, yet righ side is just evaluated and returned as such. JS 中的左侧确实被解释器自动转换为布尔值,而右侧只是被评估并返回。

I'd like to achieve something like我想实现类似

public Foo Do(Foo arg)
...
var foo = _fooProvider.GetFoo();
return foo != null && Do(foo);

Without using if-else or ternary operator.不使用 if-else 或三元运算符。

What you are really asking about is logical operator "short-circuiting" where if the first condition in a multiple condition test fails, the statement processing stops.您真正要问的是逻辑运算符“短路”,如果多条件测试中的第一个条件失败,则语句处理将停止。

C# works just as JavaScript does in this regard: C# 在这方面的工作方式与 JavaScript 一样

& Logical AND
&& Logical AND with short-circuiting
| Logical OR
|| Logical OR with short-circuiting

If you want an OO solution to your problem.如果你想要一个面向对象的解决方案来解决你的问题。 Why not implement the null patterns?为什么不实现 null 模式?

I do not know if Foo has an interface in your situation or if that is reasonabily implementable.我不知道 Foo 在您的情况下是否有接口,或者是否可以合理实现。

But instead of littering your code with null checks and maybe have all that null behaviour loatering around.但是,与其用 null 检查乱扔代码,不如让所有 null 行为四处游荡。 (like if object is null log something) (比如如果 object 是 null 记录一些东西)

Do something like做类似的事情

public interface IFoo { void Do(); }

public class Foo : IFoo { void Do() { /* all that stuff you wanna do */ } }

public class NullFoo : IFoo { void Do { /*log that do cannot be called on nullFoo because Foo with Code X could not be found */ } }

Foo probably comes from a repo or factory, when you see it cannot be instantiated. Foo 可能来自仓库或工厂,当您看到它无法实例化时。 Instead of having the client check for null, just return NullFoo, let it call Do which does nothing or log that it could not be found, and go on with its day:)与其让客户端检查 null,不如返回 NullFoo,让它调用 Do 什么都不做或记录它找不到,然后 go 就在它的一天:)

Hope this helps!希望这可以帮助!

The && operator in C# works just like in JavaScript, but only for bool operands. C# 中的 && 运算符的工作方式与 JavaScript 中的一样,但仅适用于bool操作数。 You can have operands of any type in JavaScript. JavaScript 中可以有任何类型的操作数。

JavaScript uses type coercion - it assigns boolean values (true/false) to non-bool variables. JavaScript 使用类型强制 - 它将 boolean 值(真/假)分配给非布尔变量。

0               -> false
non-0 number    -> true
[]              -> false
non-empty array -> true
undefined, null -> false
{}              -> true

x && y in JS works like this: If x coerces to false, x is returned. JS 中的x && y是这样工作的:如果 x 强制为 false,则返回 x。 If x coerces to true, y is returned.如果 x 强制为真,则返回 y。

C# works the same, just with bool operands only. C# 的工作方式相同,只是仅使用 bool 操作数。 If x is false, x (false) is returned.如果 x 为假,则返回 x (假)。 If x is true and y is false, y (false) is returned.如果 x 为真且 y 为假,则返回 y(假)。 If x is true and y is true, y (true) is returned.如果 x 为真且 y 为真,则返回 y (true)。

Now this isn't really useful for writing short code like in JS.现在这对于编写像 JS 这样的短代码并不是很有用。 Probably the best you can do is if (foo != null) processFoo(foo) or ternary foo == null? getNewFoo(): processFoo(foo)可能你能做的最好的事情是if (foo != null) processFoo(foo)或三元foo == null? getNewFoo(): processFoo(foo) foo == null? getNewFoo(): processFoo(foo) . foo == null? getNewFoo(): processFoo(foo)

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

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