By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,703 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,703 IT Pros & Developers. It's quick & easy.

what's the difference

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
* "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/>
Nov 21 '05 #2

P: n/a
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.

Nov 21 '05 #3

P: n/a
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
Nov 21 '05 #4

P: n/a
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
Nov 21 '05 #5

P: n/a
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.

Nov 21 '05 #6

P: n/a
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
Nov 21 '05 #7

P: n/a
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

Nov 21 '05 #8

P: n/a
Cor Ligthert wrote:
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"

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
Nov 21 '05 #9

P: n/a
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
Nov 21 '05 #10

P: n/a
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
Nov 21 '05 #11

P: n/a
* 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/>
Nov 21 '05 #12

P: n/a
Nak
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.
Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.