C#7: Underscore ( _ ) & Star ( * ) in Out variable

后端 未结 6 635
情书的邮戳
情书的邮戳 2020-11-27 17:08

I was reading about new out variable features in C#7 here. I have two questions:

  1. It says

    We allow \"discards\" as out parameters as w

6条回答
  •  心在旅途
    2020-11-27 17:21

    Regarding the first question

    I guess this is just an info and not a new feature of C#7 because we can do so in pre C#7.0 too.

    var _;
    if (Int.TryParse(str, out _))
        // ...
    

    The novelty is that you dont have to declare _ anymore inside or outside the expression and you can just type

    int.TryParse(s, out _);
    

    Try to do this one liner pre C#7:

    private void btnDialogOk_Click_1(object sender, RoutedEventArgs e)
    {
         DialogResult = int.TryParse(Answer, out _);
    }
    

提交回复
热议问题