VB.NET equivalent for C# 7 Type pattern matching

前端 未结 2 654
甜味超标
甜味超标 2021-01-17 09:37

Is there a VB.NET equivalent to this? Note in particular the bmp in the code sample.

public void MyMethod(Object obj)
{
    if (obj is          


        
2条回答
  •  一生所求
    2021-01-17 10:39

    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;
    

提交回复
热议问题