What are the Java equivalents in C # 'is' and 'as'?

The question says it all. Some quick code examples would be nice .. thanks!

+3
source share
2 answers

is=> instanceof( JLS link ), for example:

Object foo = "hello";
if (foo instanceof String) {
  // Yup, it a string
}

In Java, there is no equivalent to the C # operator as.

+8
source

is(C #) → instanceof(Java)

And you do not get the direct equivalent as. You could try this liner though:

SomeParentType obj = 
    original instanceof Child ? (SomeParentType)original : null;
+2
source

Source: https://habr.com/ru/post/1736324/


All Articles