请向 vb 用户解释 C# 语法
我有以下代码片段:
// Notify the source (the other control).
if (operation != DropOperation.Reorder) {
e = new DroppedEventArgs()
{
Operation = operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere,
Source = src,
Target = this,
DroppedItems = srcItems
};
src.OnDropped(e);
}
我不明白操作 = 操作 == DropOperation.MoveToHere ? DropOperation.MoveFromHere :DropOperation.CopyFromHere
行。
有人可以解释一下吗?根据记录...dropOperation 是一个枚举。 我需要的只是你能给出 VB 语法等效项吗?
赛斯
I have the following code snippet:
// Notify the source (the other control).
if (operation != DropOperation.Reorder) {
e = new DroppedEventArgs()
{
Operation = operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere,
Source = src,
Target = this,
DroppedItems = srcItems
};
src.OnDropped(e);
}
I do not understand theOperation = operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere
line.
Can someone explain it? For the record...dropOperation is an enum.
Can you give vb syntactical equivalent is all I need.
Seth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
之所以难以理解是因为你不熟悉三元运算符 ?: 。基本上,它的作用是评估一个表达式,并根据评估返回的是 true 还是 false 返回两个值之一。
例如,如果布尔值为 true,则以下表达式将返回“true”,否则返回“false”:
事实上,它也存在于 VB.NET 中 - 但表达方式略有不同。这两个语句分别在 C# 和 VB.NET 中实际上是相同的。
IIf 和十元运算符之间的区别在于 IIf 将始终计算第二个和第三个参数,因为 IIf 是函数而不是运算符。因此,更倾向于使用 Tenary 算子。
注意:十元运算符是在 VB 9 中添加的,因此如果您使用的是以前的版本,则必须依赖 IIF 函数来实现此功能。
The reason it's hard to understand is due to the fact that you're unfamiliar with the ternary operator ?:. Basically what it does is evaluate an expression, and return one of two value depending on whether the evaluation returned true or false.
For example, the following expression will return "true" if the boolean is true, and "false" elsewise:
It does in fact exist in VB.NET as well - expressed a bit differently though. These two statements in respectively C# and VB.NET are in fact the same
The difference between IIf and the tenary operator is that IIf will always evaluate both the second and third parameter because IIf is a function instead of an operator. For this reason the tenary operator is much to prefer.
Note: The tenary operator was added in VB 9, so if you're using previous versions you'll have to rely on the IIF function for this functionality.
强制性维基百科链接。我放弃在评论中提及这个链接,所以这里是一个答案。您可以替换 ? 的用途调用 IIF 函数的运算符:
请注意,它们并不严格等效,因为 IIF 函数会评估 true 和 false 情况,而 ? 运算符仅评估其返回的情况。
Obligatory wikipedia link. I gave up on mentioning this link in a comment, so here it is in an answer. You can replace uses of the ? operator with calls to the IIF function:
Note that they are not strictly equivalent, since the IIF function evaluates both the true and the false case, whereas the ? operator only evaluates the case it returns.
它有点相当于
IIf VB.NET 中的
函数(请参阅 Brian 的评论):在 C# 中,这称为 条件运算符,是简单 if/else 语句的一种快捷方式。
It is sort of equivalent of the
IIf
function in VB.NET (see Brian's comment):In C# this is called the conditional operator, and is a sort of shortcut for a simple if/else statement.
这是条件运算符,它与VB的非常相似
IIf
函数:在此特定示例中,
IIf
函数将编写如下:This is the conditional operator, it is very similar to VB's
IIf
function:In this particular example the
IIf
function would be written like this:这是使用
?
条件赋值运算符。这行基本上是语法糖:在 VB 中,相当于:
This is using the
?
operator for conditional assignment. This line is basically syntactic sugar for:Which, in VB, would be equivalent to:
这称为三元运算符。这基本上是一种简短的写法:
This is called the ternary operator. It's basically a short way of writing:
?:
结构是三元运算符,基本上是内联if (x) y else x
。内联的好处可以在这里看到,因为它会立即分配给变量。你不能用 if 语句来做到这一点。The
?:
construct is the ternary operator, basically an inlineif (x) y else x
. The benefit of the inline is seen here in that it is assigned immediately to a variable. You can't do that with an if statement.C# 博主使用“?”很多。看这段代码:
等于:
只需阅读解释良好的甜甜圈的答案!
(“VB-er”我喜欢这个词)
C# Bloggers use the "?" a lot. Look this code:
Is equal to:
Just read the well explained Donut's answer!!
("VB-er" I like the term)
它被称为三元运算符。我不知道我认为 VB 中不存在它,但它基本上只是 if/else 的简写。
It's called the ternary operator. I don't think it exists in VB but it's basically just a shorthand for an if/else.