[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

?? operator in C#

Dear Experts,

I want to know if I can do this in C#

MyClass
{
   public bool ShowHeader {return true;}
}

AnotherClass
{
   MyClass myClass = GetInstanceOfMyClass()

   return myClass.ShowHeader ?? false;
}

Basically if the myClass object is null i want to return false otherwise the value of myClass.SHowHeader
0
vbhargav80
Asked:
vbhargav80
3 Solutions
 
oxyooCommented:
I would recommend using the ?: operator as shown below. What you want is not possible using the ?? operator as far as I know (but I could be wrong).
Good Luck!

AnotherClass
{
   public bool MethodName() {
      MyClass myClass = GetInstanceOfMyClass()
      return myClass != null ? myClass.ShowHeader : false;
   }
}

Open in new window

0
 
tetorvikCommented:
Haven't seen such an operator, but for sure you can do something like:
return myClass == null ? false : myClass.ShowHeader;

Open in new window

0
 
DhaestCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
evilrixSenior Software Engineer (Avast)Commented:
>> Basically if the myClass object is null i want to return false otherwise the value of myClass.SHowHeader
The type false (bool) and MyClass are not compatible. The correct thing to do would to be to return null or throw an exception if the failed creation if the class is considered an error condition.
0
 
alb66Commented:
return myClass == null ? false : myClass.ShowHeader();

But, in my opinion, it is more clear if you write:

if ( myClass != null )
  return myClass.ShowHeader();
return false;
0
 
evilrixSenior Software Engineer (Avast)Commented:
^^^ if you function returns a bool type (which I've just noticed it does after re-reading the question :) ), then any of the above answers should suffice... but as I said, if the creation of a class fails then you should consider throwing an exception if this is an error condition and not an expected state.
0
 
vbhargav80Author Commented:
Hi oxyoo,

The only doubt I have is this. If myClass is null, wouldn't myClass.ShowHeader throw an NullReferenceexception?
0
 
oxyooCommented:
@vbhargav80
Well the code states if myClass is not (!=) null then it will call ShowHeader. If it is null then it will return false and ShowHeader will not be called at all. So there shouldn't be any problem.
@alb66
The if-statement is more readable I guess, but I favor (as far as possible) to use only one exit point in a method to reduce complexity and make it less error prone. Not a big deal in a small method like this perhaps. Perhaps a matter of  "design-taste" :)


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now