IIF equivalent in C#(blog.emanuelebartolesi.com)
submitted by kasuken(275) 3 years, 11 months ago
The equivalent operator in C# for IIF.
4 comments |category: C# |Views: 205 Tweet
tags: Wrong C# another
Add a live kick counter to your blog >>
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:
Simply copy and paste this HTML into your blog post.
posted by gregbeech(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 zowens(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)
posted by drugarce(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
posted by rockinthesixstring(255) 3 years, 4 months ago 0
http://www.dotnetblogger.com/post/2009/01/05/VBNET-Inline-IF-Statements.aspx
All tags Your tags
Suggest a new category
dpeterson(1273)
Telerik(409)
fdub(381)
amit.jain(268)
BlackWasp(259)
RobertTheGrey(259)
jalpesh(252)
bsenoff(240)
KodefuGuru(227)
KMillerr(226)