IIF equivalent in C#(blog.emanuelebartolesi.com)

submitted by kasukenkasuken(275) 3 years, 11 months ago

The equivalent operator in C# for IIF.

4 comments |category: |Views: 205

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by gregbeechgregbeech(76) 3 years, 11 months ago 0

Actually the C# ternary operator isn't equivalent to IIF in VB, because the IIF function always evaluates both the true and false actions, whereas the ?: operator is a lazily evaluated left-associative operator which only associates the appropriate action. e.g. IIf(a IsNot Nothing, a.ToLower(), String.Empty) will throw a NullReferenceException in VB if a is null because it will try to evaluate a.ToLower() anyway, whereas a != null ? a.ToLower() : string.Empty will not throw in C#. The C# ternary operator didn't have an equivalent in VB until VB9, where you can now write the rather confusing If(a IsNot Nothing, a.ToLower(), String.Empty).

Reply

posted by zowenszowens(988) 3 years, 11 months ago 0

Logically it is the same, but it looks like it isn't technically the same (good tip gregbeech)

Reply

posted by drugarcedrugarce(610) 3 years, 11 months ago 0

IIf is evil. Other then the reason mentioned by gregbeech, the other thing is that it returns an Object so in most cases you would need to cast it before assigning the evaluated expression

Reply

information Login or create an account to comment on this story