C# 中的三元运算符

发布于 2024-08-31 09:59:21 字数 288 浏览 6 评论 0原文

使用三元运算符,可以执行如下操作(假设 Func1() 和 Func2() 返回一个 int:

int x = (x == y) ? Func1() : Func2();

但是,有没有办法在不返回值的情况下执行相同的操作?例如,类似 (假设 Func1() 和 Func2() 返回 void):

(x == y) ? Func1() : Func2();

我意识到这可以使用 if 语句来完成,我只是想知道是否有办法这样做。

With the ternary operator, it is possible to do something like the following (assuming Func1() and Func2() return an int:

int x = (x == y) ? Func1() : Func2();

However, is there any way to do the same thing, without returning a value? For example, something like (assuming Func1() and Func2() return void):

(x == y) ? Func1() : Func2();

I realise this could be accomplished using an if statement, I just wondered if there was a way to do it like this.

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

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

发布评论

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

评论(4

划一舟意中人 2024-09-07 09:59:21

很奇怪,但你可以做

class Program
{
    private delegate void F();

    static void Main(string[] args)
    {
        ((1 == 1) ? new F(f1) : new F(f2))();
    }

    static void f1()
    {
        Console.WriteLine("1");
    }

    static void f2()
    { 
        Console.WriteLine("2");
    }
}

Weird, but you could do

class Program
{
    private delegate void F();

    static void Main(string[] args)
    {
        ((1 == 1) ? new F(f1) : new F(f2))();
    }

    static void f1()
    {
        Console.WriteLine("1");
    }

    static void f2()
    { 
        Console.WriteLine("2");
    }
}
日裸衫吸 2024-09-07 09:59:21

我不这么认为。据我记得,三元运算符用于表达式上下文,而不是用作语句。 编译器需要知道表达式的类型,而void并不是真正的类型。

您可以尝试为此定义一个函数:

void iif(bool condition, Action a, Action b)
{
    if (condition) a(); else b();
}

然后您可以这样调用它:

iif(x > y, Func1, Func2);

但这并不能真正使您的代码更清晰......

I don't think so. As far as I remember, the ternary operator is used in an expression context and not as a statement. The compiler needs to know the type for the expression and void is not really a type.

You could try to define a function for this:

void iif(bool condition, Action a, Action b)
{
    if (condition) a(); else b();
}

And then you could call it like this:

iif(x > y, Func1, Func2);

But this does not really make your code any clearer...

江心雾 2024-09-07 09:59:21

如果您有信心,您可以创建一个静态方法,其唯一目的是吸收表达式并“使其成为”一条语句。

public static class Extension
{
    public static void Do(this Object x) { }
}

通过这种方式,您可以调用三元运算符并调用其扩展方法。

((x == y) ? Func1() : Func2()).Do(); 

或者,以几乎等效的方式,编写静态方法(如果要使用此“快捷方式”的类受到限制)。

private static void Do(object item){ }

...并以这种方式调用它

Do((x == y) ? Func1() : Func2());

但是,我强烈建议不要使用这个“快捷方式”,原因与我之前的作者已经明确指出的相同。

If you feel confident, you'd create a static method whose only purpose is to absorb the expression and "make it" a statement.

public static class Extension
{
    public static void Do(this Object x) { }
}

In this way you could call the ternary operator and invoke the extension method on it.

((x == y) ? Func1() : Func2()).Do(); 

Or, in an almost equivalent way, writing a static method (if the class when you want to use this "shortcut" is limited).

private static void Do(object item){ }

... and calling it in this way

Do((x == y) ? Func1() : Func2());

However I strongly reccomend to not use this "shortcut" for same reasons already made explicit by the authors before me.

月竹挽风 2024-09-07 09:59:21

不,因为三元运算符是表达式,而 actions/void 函数是语句。您可以让它们返回object,但我认为 if/else 块将使意图更加清晰(即,执行操作是为了它们的副作用而不是它们的值)。

No, because the ternary operator is an expression, whereas actions/void functions are statements. You could make them return object, but I think that an if/else block would make the intent much clearer (i.e. the actions are being executed for their side-effects instead of their values).

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