what's the difference between using the VB.NET function IIF or the c#
operator ?
VB.NET: a = iif ( b=c, d, e)
C#: a= (b==c)?d:e
TIA, z. 12 1742
* "z. f." <zi**@info-scopeREMSPAM.co.il> scripsit: what's the difference between using the VB.NET function IIF or the c# operator ? VB.NET: a = iif ( b=c, d, e) C#: a= (b==c)?d:e
In VB.NET, 'd' and 'e' will be evaluated and boxed in an 'Object'.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
z. f.
Last I checked, the IIF was a function in VB, not an operator, so it had
the side effect of evaluating the false condition, even when the condition
was true. So if you did something like this:
a = Iif(b = c, GetD(), GetE())
Then both GetD and GetE would be called, but only one would be returned.
This can have nasty side effects.
In C#, the ? operator will not evaluate the other condition if it is not
met.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"z. f." <zi**@info-scopeREMSPAM.co.il> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl... what's the difference between using the VB.NET function IIF or the c# operator ? VB.NET: a = iif ( b=c, d, e) C#: a= (b==c)?d:e
TIA, z.
z. f. wrote: what's the difference between using the VB.NET function IIF or the c# operator ? VB.NET: a = iif ( b=c, d, e) C#: a= (b==c)?d:e
Just look at the IL code (appended)
In short: C# has the faster solution.
VB.NET:
..maxstack 3
.locals init ([0] int16 a,
[1] int16 b,
[2] int16 c,
[3] int16 d,
[4] int16 e)
IL_0000: nop
IL_0001: ldloc.1
IL_0002: ldloc.2
IL_0003: ceq
IL_0005: ldloc.3
IL_0006: box [mscorlib]System.Int16
IL_000b: ldloc.s e
IL_000d: box [mscorlib]System.Int16
IL_0012: call object [Microsoft.VisualBasic]
Microsoft.VisualBasic.Interaction::IIf(bool, object, object)
IL_0017: call int16 [Microsoft.VisualBasic]
Microsoft.VisualBasic.CompilerServices.ShortType:: FromObject(object)
IL_001c: stloc.0
IL_001d: nop
IL_001e: ret
C#:
..maxstack 2
.locals init ([0] int32 a,
[1] int32 b,
[2] int32 c,
[3] int32 d,
[4] int32 e)
IL_0000: ldc.i4.0
IL_0001: stloc.1
IL_0002: ldc.i4.0
IL_0003: stloc.2
IL_0004: ldc.i4.0
IL_0005: stloc.3
IL_0006: ldc.i4.0
IL_0007: stloc.s e
IL_0009: ldloc.1
IL_000a: ldloc.2
IL_000b: beq.s IL_0011
IL_000d: ldloc.s e
IL_000f: br.s IL_0012
IL_0011: ldloc.3
IL_0012: stloc.0
IL_0013: ret
--
Greetings
Jochen
z. f. <zi**@info-scopeREMSPAM.co.il> wrote: what's the difference between using the VB.NET function IIF or the c# operator ? VB.NET: a = iif ( b=c, d, e) C#: a= (b==c)?d:e
Firstly, iif evaluates every function in every expression - C# only
evalutes the "testing" expression and then whichever of the expressions
is going to be returned. Here's a pair of complete programs to show the
difference:
C#:
using System;
public class Test
{
static void Main()
{
int x = ReturnTrue() ? Return1() : Return2();
}
static bool ReturnTrue()
{
Console.WriteLine ("ReturnTrue called");
return true;
}
static int Return1()
{
Console.WriteLine ("Return1 called");
return 1;
}
static int Return2()
{
Console.WriteLine ("Return2 called");
return 2;
}
}
VB.NET:
Option Strict On
Imports System
Imports Microsoft.VisualBasic
Class Test
Shared Sub Main
Dim x as Object = IIf (ReturnTrue, Return1, Return2)
End Sub
Shared Function ReturnTrue As Boolean
Console.WriteLine ("ReturnTrue called")
ReturnTrue = true
End Function
Shared Function Return1 As Integer
Console.WriteLine ("Return1 called")
Return1 = 1
End Function
Shared Function Return2 As Integer
Console.WriteLine ("Return2 called")
Return2 = 2
End Function
End Class
Output from C# version:
ReturnTrue called
Return1 called
Output from VB.NET version:
ReturnTrue called
Return1 called
Return2 called
Secondly, the conditional operator in C# has specific requirements
about the types of the second and third expressions which allow the
expression as a whole to have an appropriate type (rather than just the
Object of VB.NET's IIF).
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
z.
As Hefried suggests, iif in VB.NET is a function call.
It actually & directly calls a function in the
Microsoft.VisualBasic.Interaction module, because you are calling a
function, both arguments have to be evaluated.
Where as operator ? is an operator, it is compiled directly into IL code.
because it is IL code, it is able to short circuit and only evaluate the
argument that needs to be returned.
Hope this helps
Jay
"z. f." <zi**@info-scopeREMSPAM.co.il> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl... what's the difference between using the VB.NET function IIF or the c# operator ? VB.NET: a = iif ( b=c, d, e) C#: a= (b==c)?d:e
TIA, z.
ZF,
Why you asking this. VB.NET: a = iif ( b=c, d, e)
Can only be used in VBNet
C#: a= (b==c)?d:e
Can only be used in C#
What is the relevance of your question?
Cor
Patty,
VB can not use the ? syntax. As for if it is a "more poor" language
than C#, well, I'm staying out of that one.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com... can VB.NET not use the ? : syntax? If so, then use it, if not - that's another reason why it's more of a poor language than C#.
"Jochen Kalmbach" wrote:
z. f. wrote:
> what's the difference between using the VB.NET function IIF or the c# > operator ? > VB.NET: a = iif ( b=c, d, e) > C#: a= (b==c)?d:e
Just look at the IL code (appended)
In short: C# has the faster solution. VB.NET: ..maxstack 3 .locals init ([0] int16 a, [1] int16 b, [2] int16 c, [3] int16 d, [4] int16 e) IL_0000: nop IL_0001: ldloc.1 IL_0002: ldloc.2 IL_0003: ceq IL_0005: ldloc.3 IL_0006: box [mscorlib]System.Int16 IL_000b: ldloc.s e IL_000d: box [mscorlib]System.Int16 IL_0012: call object [Microsoft.VisualBasic] Microsoft.VisualBasic.Interaction::IIf(bool, object, object) IL_0017: call int16 [Microsoft.VisualBasic] Microsoft.VisualBasic.CompilerServices.ShortType:: FromObject(object) IL_001c: stloc.0 IL_001d: nop IL_001e: ret
C#: ..maxstack 2 .locals init ([0] int32 a, [1] int32 b, [2] int32 c, [3] int32 d, [4] int32 e) IL_0000: ldc.i4.0 IL_0001: stloc.1 IL_0002: ldc.i4.0 IL_0003: stloc.2 IL_0004: ldc.i4.0 IL_0005: stloc.3 IL_0006: ldc.i4.0 IL_0007: stloc.s e IL_0009: ldloc.1 IL_000a: ldloc.2 IL_000b: beq.s IL_0011 IL_000d: ldloc.s e IL_000f: br.s IL_0012 IL_0011: ldloc.3 IL_0012: stloc.0 IL_0013: ret -- Greetings Jochen
Nicholas,
In my opinion are they both as legacy as an operator can be.
When that would give an opinion of a language, it is absolute time for a
complete new one.
Cor
ZF
Jochen did give an difference VB.NET: a = iif ( b=c, d, e)
Can be used in every Net language
C#: a= (b==c)?d:e
Can only be used in languages that have legacy C operators.
Cor
* Jochen Kalmbach <no********************@holzma.de> scripsit: VB.NET: a = iif ( b=c, d, e) Can only be used in VBNet
This you can also use in ANY .NET language... just reference the VB assembly and call "Microsoft.VisualBasic.Interaction::IIf"
ACK. And 'IIf' can be easily reimplemented in every .NET programming
language.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Hi, can VB.NET not use the ? : syntax? If so, then use it, if not - that's another reason why it's more of a poor language than C#.
LOL, apparently the new version of VB to come will let you define your own
operators, or so I read somewhere. But that does not make if a poor
language for now just because you will have to write a few more lines of
code, I mean it even indents it for you and writes the "End If" for you so
it isn't *that* hard to do!
Nick. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from...
|
by: mike |
last post by:
best regards:
What is the difference between DOM Level 1 and DOM Level 2.
Thank you.
May god be with you.
|
by: radnus2004 |
last post by:
Hi all,
I am not clear what is the difference between signed and unsigned
in C. Many say, unsigned means only +ve values and 0.
Can I use unsigned int i = -3?
What happens internally ? What...
|
by: tinesan |
last post by:
Hello fellow C programmers,
I'm just learning to program with C, and I'm wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the...
|
by: Jason Huang |
last post by:
Hi,
Would someone explain the following coding more detail for me? What's the
( ) for?
CurrentText = (TextBox)e.Item.Cells.Controls;
Thanks.
Jason
|
by: Nathan Sokalski |
last post by:
What is the difference between the Page_Init and Page_Load events? When I
was debugging my code, they both seemed to get triggered on every postback.
I am assuming that there is some difference,...
|
by: Anoj Kumar |
last post by:
Hi All ,
can anyone tell me what is the difference between the following declaration
and how it affects
application performance.
1.
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
|
by: Mark Kamoski |
last post by:
Hi Everyone--
Please help.
What is the difference between a Field and a Property?
Here is the context.
In the VS.NET 2002 documentation, in the "String Members" section, we have
the...
|
by: kai |
last post by:
Hi,
In ASP.NET , what is the difference between OnClick and Click events for a
button? Because we have button click event, it can trigger events, why we
still need OnClick?
Please help.
...
|
by: Zach |
last post by:
After having taken a looong break from VB (last used 6.0), I started
getting back into programming again, this time with VS 2005. I began
reading up on VB.NET from various sources (books,...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |