有什么不同 [英] what's the difference
问题描述
使用VB.NET函数IIF或c#
运算符之间的区别是什么?
VB.NET:a = iif(b = c, d,e)
C#:a =(b == c)?d:e
TIA,z。
*" z。 F"。 <滋** @ info-scopeREMSPAM.co.il> scripsit:使用VB.NET函数IIF或c#
运算符之间的区别是什么?
VB.NET:a = iif(b = c,d ,e)
C#:a =(b == c)?d:e
在VB.NET中,''d''和'' e''将被评估并装入''对象''。
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>
ž。 f。
最后我查了一下,IIF是VB中的一个函数,而不是一个操作符,所以它有
评估错误条件的副作用,即使条件
为真。所以如果你做了这样的事情:
a = Iif(b = c,GetD(),GetE())
然后两个GetD并且会调用GetE,但只返回一个。
这可能会产生令人讨厌的副作用。
在C#中,?如果不符合
,运营商将不会评估其他条件。
希望这会有所帮助。
-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard。 caspershouse.com
" z。 F"。 <滋** @ info-scopeREMSPAM.co.il>在消息中写道
新闻:eA ************** @ TK2MSFTNGP09.phx.gbl ...这是什么使用VB.NET函数IIF或c#
运算符之间的区别?
VB.NET:a = iif(b = c,d,e)
C#:a =(b == c)?d:e
TIA,z。
z。 F。写道:
使用VB.NET函数IIF或c#
运算符之间的区别是什么?
VB.NET:a = iif(b = c,d,e)
C#:a =(b == c)?d:e
看看IL代码(附后)
简而言之:C#有更快的解决方案。
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]系统。 Int16
IL_000b:ldloc.se
IL_000d:box [mscorlib] System.Int16
IL_0012:调用对象[Microsoft.VisualBasic]
Microsoft.VisualBasic.Interaction :: IIf(bool,object,object)
IL_0017:调用int16 [Microsoft.VisualBasic]
Micr osoft.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.se
IL_0009:ldloc.1
IL_000a:ldloc.2
IL_000b: beq.s IL_0011
IL_000d:ldloc.se
IL_000f:br.s IL_0012
IL_0011:ldloc.3
IL_0012:stloc.0
IL_0013:ret
-
问候
Jochen
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." <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
这篇关于有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!