请向 vb 用户解释 C# 语法

发布于 2024-08-04 17:19:42 字数 681 浏览 6 评论 0原文

我有以下代码片段:

        // 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 the
Operation = 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 技术交流群。

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

发布评论

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

评论(10

简单气质女生网名 2024-08-11 17:19:42

之所以难以理解是因为你不熟悉三元运算符 ?: 。基本上,它的作用是评估一个表达式,并根据评估返回的是 true 还是 false 返回两个值之一。

例如,如果布尔值为 true,则以下表达式将返回“true”,否则返回“false”:

bool test = false;
string testString = test ? "true" : "false";

事实上,它也存在于 VB.NET 中 - 但表达方式略有不同。这两个语句分别在 C# 和 VB.NET 中实际上是相同的。

Dim s As String = If(True, "kek", "lol")
string s = true ? "kek" : "lol";

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:

bool test = false;
string testString = test ? "true" : "false";

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

Dim s As String = If(True, "kek", "lol")
string s = true ? "kek" : "lol";

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.

浅唱ヾ落雨殇 2024-08-11 17:19:42
If (operation = DropOperation.MoveToHere) Then
    Operation = DropOperation.MoveFromHere
Else
    Operation = DropOperation.CopyFromHere
End If
If (operation = DropOperation.MoveToHere) Then
    Operation = DropOperation.MoveFromHere
Else
    Operation = DropOperation.CopyFromHere
End If
弥繁 2024-08-11 17:19:42

强制性维基百科链接。我放弃在评论中提及这个链接,所以这里是一个答案。您可以替换 ? 的用途调用 IIF 函数的运算符:

Operation = IIF(operation = DropOperation.MoveToHere, DropOperation.MoveFromHere, DropOperation.CopyFromHere)

请注意,它们并不严格等效,因为 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:

Operation = IIF(operation = DropOperation.MoveToHere, DropOperation.MoveFromHere, DropOperation.CopyFromHere)

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.

你是暖光i 2024-08-11 17:19:42

有点相当于IIf VB.NET 中的 函数(请参阅 Brian 的评论):

Operation = IIf(operation = DropOperation.MoveToHere, _
                DropOperation.MoveFromHere, _
                DropOperation.CopyFromHere)

在 C# 中,这称为 条件运算符,是简单 if/else 语句的一种快捷方式。

It is sort of equivalent of the IIf function in VB.NET (see Brian's comment):

Operation = IIf(operation = DropOperation.MoveToHere, _
                DropOperation.MoveFromHere, _
                DropOperation.CopyFromHere)

In C# this is called the conditional operator, and is a sort of shortcut for a simple if/else statement.

柒夜笙歌凉 2024-08-11 17:19:42

这是条件运算符,它与VB的非常相似IIf 函数:

根据表达式的计算结果,返回两个对象之一。

Public Function IIf( _
   ByVal Expression As Boolean, _ 
   ByVal TruePart As Object, _ 
   ByVal FalsePart As Object _ 
) As Object

在此特定示例中,IIf 函数将编写如下:

Operation = IIF((operation = DropOperation.MoveToHere), _
    DropOperation.MoveFromHere, _
    DropOperation.CopyFromHere)

This is the conditional operator, it is very similar to VB's IIf function:

Returns one of two objects, depending on the evaluation of an expression.

Public Function IIf( _
   ByVal Expression As Boolean, _ 
   ByVal TruePart As Object, _ 
   ByVal FalsePart As Object _ 
) As Object

In this particular example the IIf function would be written like this:

Operation = IIF((operation = DropOperation.MoveToHere), _
    DropOperation.MoveFromHere, _
    DropOperation.CopyFromHere)
萌吟 2024-08-11 17:19:42

这是使用 ?条件赋值运算符。这行基本上是语法糖:

// C# expanded example
if (operation == DropOperation.MoveToHere)
{
   Operation = DropOperation.MoveFromHere;
}
else
{
   Operation = DropOperation.CopyFromHere;
}

在 VB 中,相当于:

If operation = DropOperation.MoveToHere Then
   Operation = DropOperation.MoveFromHere
Else
   Operation = DropOperation.CopyFromHere
End If

This is using the ? operator for conditional assignment. This line is basically syntactic sugar for:

// C# expanded example
if (operation == DropOperation.MoveToHere)
{
   Operation = DropOperation.MoveFromHere;
}
else
{
   Operation = DropOperation.CopyFromHere;
}

Which, in VB, would be equivalent to:

If operation = DropOperation.MoveToHere Then
   Operation = DropOperation.MoveFromHere
Else
   Operation = DropOperation.CopyFromHere
End If
∞觅青森が 2024-08-11 17:19:42
operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere

这称为三元运算符。这基本上是一种简短的写法:

if (operation == DropOperation.MoveToHere)
  return DropOperation.MoveToHere;
else
  return DropOperation.CopyFromHere;
operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere

This is called the ternary operator. It's basically a short way of writing:

if (operation == DropOperation.MoveToHere)
  return DropOperation.MoveToHere;
else
  return DropOperation.CopyFromHere;
戴着白色围巾的女孩 2024-08-11 17:19:42

?: 结构是三元运算符,基本上是内联 if (x) y else x。内联的好处可以在这里看到,因为它会立即分配给变量。你不能用 if 语句来做到这一点。

The ?: construct is the ternary operator, basically an inline if (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.

执手闯天涯 2024-08-11 17:19:42

C# 博主使用“?”很多。看这段代码:

int Foo(int x, int y){
return x==y? 10: 11;
}

等于:

int Foo(int x, int y){
if (x==y)
return 10; 
else
return 11;
}

只需阅读解释良好的甜甜圈的答案!

(“VB-er”我喜欢这个词)

C# Bloggers use the "?" a lot. Look this code:

int Foo(int x, int y){
return x==y? 10: 11;
}

Is equal to:

int Foo(int x, int y){
if (x==y)
return 10; 
else
return 11;
}

Just read the well explained Donut's answer!!

("VB-er" I like the term)

烙印 2024-08-11 17:19:42

它被称为三元运算符。我不知道我认为 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.

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