C#'s 'is' equivalent in D

drug drug2004 at bk.ru
Thu Oct 10 15:52:54 UTC 2019


On 10/10/19 6:47 PM, Just Dave wrote:
> In C# you can do something like:
> 
> 
>      if (obj is Person)
>      {
>          var person = obj as Person;
>          // do stuff with person...
>      }
> 
> where you can check the type of an object prior to casting. Does D have 
> a similar mechanism? It's so widely useful in the C# realm that they 
> even added syntactic sugar to allow:
> 
>      if (obj is Person person)
>      {
>          // do stuff with person...
>      }
> 
> I would presume since D has reference objects there must exist some 
> mechanism for this...

```D
       if (auto person = cast(Person) obj)
       {
           // do stuff with person...
       }
```


More information about the Digitalmars-d-learn mailing list