jeudi 20 octobre 2016

java and c# avoid nested if for null and parameter condition

I would like to know if it is possible to avoid nested IF statements while I want to check if variable/object is null and if variable/object meet some condition eg.

var obj = "test";
if(obj!=null){
   if(obj.equals("test"){
      do something;
   }
}

Is it possible to do oneliner of this statement without defining own method which handle this ?

if(obj!=null && obj.equals("test")) {}

will obviously fail if obj is null.

Thank you.

Aucun commentaire:

Enregistrer un commentaire