VB.NET equivalent for the C# 7 is operator declaration pattern

Viewed 2140

Is there a VB.NET equivalent to the C# 7 is operator declaration pattern? Note in particular the bmp in the following code sample:

public void MyMethod(Object obj)
{
    if (obj is Bitmap bmp)
    {
        // ...
    }
}

Or the short pattern matching syntax with is is exclusive to C#?

EDIT:

I already know these syntaxes:

    If TypeOf obj Is Bitmap Then
        Dim bmp As Bitmap = obj
        ' ...
    End If

or

    Dim bmp As Bitmap = TryCast(obj, Bitmap)
    If bmp IsNot Nothing Then
        ' ...
    End If

What I want to know is whether there is something even shorter, like that new C#7 is operator declaration pattern...

Thank you very much.

2 Answers

Use a one line if

If obj is bitmap Then Dim bmp = obj

or use an in-line if (this is the if function)

Dim bmp = If(obj is bitmap, obj, Nothing)

Not quite pattern-matching per se, but is does the same thing.

Couldn't you do it this way in C#:

var bmp = obj is bitmap ? obj : nothing;
Related