DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • GenAI Implementation Isn't Magic — It’s a Lifecycle
  • Is the Data Warehouse Dead? 3 Patterns From Enterprise Architecture That Answer This Question
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Skills, Java 17, and Theme Accents

What's The IIF In C#

By 
Snippets Manager user avatar
Snippets Manager
·
Dec. 14, 11 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free
C# has the "?" ternary operator, like other C-style languages. However, this is not perfectly equivalent to iif. There are two important differences.

To explain the first, this iif() call would cause a DivideByZero exception even though the expression is true because iif is just a function and all arguments must be evaluated before calling:

iif(true, 1, 1/0)

Another way, iif does not short circuit in the traditional sense, as your question indicates. On the other hand, this ternary expression does and so is perfectly fine:

(true)?1:1/0;

The other difference is that iif is not type safe. It accepts and returns arguments of type object. The ternary operator uses type inference to know what type it's dealing with

Eclipse IDE and Eclipse

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook