what's the difference

  • Thread starter Thread starter Herfried K. Wagner [MVP]
  • Start date Start date
H

Herfried K. Wagner [MVP]

* "z. f. said:
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'.
 
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.
 
z. f. said:
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
 
z. f. said:
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).
 
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
 
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]
- (e-mail address removed)

Patty O'Dors said:
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 said:
z. f. said:
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
 
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
 
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.
 
* Jochen Kalmbach said:
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.
 
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.
 
Back
Top