Variable not declared in the scope, but works ("inference"??)

The syntax below works in a project in Visual Studio 2017, .NET 4.5. This same syntax does not work on another machine, with Visual Studio 2015, .NET 4.5. I do not know this syntax (for me 'eventargs' does not exist in that scope). Is there any update in C # where it might be inferring 'eventargs'? I did not find anything about it...

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(value is Telerik.XamarinForms.Input.AutoComplete.SuggestionItemSelectedEventArgs eventArgs))
            throw new ArgumentException("Expected SuggestionItemSelectedEventArgs as value", nameof(value));

        return eventArgs;
    }

1 answer

  • answered 2018-01-11 19:39 Daniel A. White

    This is a C# 7 feature called pattern matching, introduced with the compiler in Visual Studio 2017.

    Basically eventArgs is declared with the is expression.