VB.NET相当于C#运算符'??' [英] VB.NET equivalent of C# Operator '??'
问题描述
Hello Newsgroup,
$ c $ b存在''??''运算符。
它的用法如下: br />
string s1 = null;
string s2 = s1 ?? "(undefined)"
并执行以下操作(简化):
if(s1 == null)
s2 ="(undefined)"
else
s2 = s1
是否有等效运算符在Visual Basic.Net中,如果是这样,它是如何调用的?
提前谢谢
Philipp
Hello Newsgroup,
in c# there exists the ''??'' operator.
It is used like this :
string s1 = null;
string s2 = s1 ?? "(undefined)"
And does the following (simplified) :
if(s1 == null)
s2 = "(undefined)"
else
s2 = s1
Is there an equivalent operator in Visual Basic.Net, and if so, how is
it called ?
Thanks in advance
Philipp
推荐答案
Philipp Brune写道:
Philipp Brune wrote:
Visual Basic中是否有等效的运算符.Net,如果是的话,怎么称它为b $ b?
Is there an equivalent operator in Visual Basic.Net, and if so, how is
it called ?
我认为你最接近的就是使用IIf:
\\\
Dim s1 As String = Nothing
Dim s2 As String = IIf(s1 Is Nothing,"(undefined)",s1).ToString
///
HTH,
-
(O)enone
I think the closest you''ll get to that is by using IIf:
\\\
Dim s1 As String = Nothing
Dim s2 As String = IIf(s1 Is Nothing, "(undefined)", s1).ToString
///
HTH,
--
(O)enone
我认为你最接近的就是使用IIf:
\\\
Dim s1 As String = Nothing
Dim s2 As String = IIf(s1 Is Nothing,(undefined) ;,s1).ToString
///
I think the closest you''ll get to that is by using IIf:
\\\
Dim s1 As String = Nothing
Dim s2 As String = IIf(s1 Is Nothing, "(undefined)", s1).ToString
///
但是,什么更具可读性?这个:
如果s1什么都没有那么
s2 ="(undefined)"
Else
s2 = s1
结束如果
或者这个:
s2 = IIf(s1 Is Nothing ,(undefined),s1).ToString
我们在工作中也讨论了这个问题并决定前者更好(在
上下文中) C ++,而不是.NET,即.z =(v< w?x:y))。这些函数可以很好地帮助混淆代码;)
Robin
Yes, however, what is more readable? This:
If s1 Is Nothing Then
s2 = "(undefined)"
Else
s2 = s1
End If
or this:
s2 = IIf(s1 Is Nothing, "(undefined)", s1).ToString
We also had this debate at work and decided the former was better (in the
context of C++, not .NET, ie. z = ( v < w ? x : y ) ). These functions do
well to help obfuscate code however ;)
Robin
Robinson写道:
Robinson wrote:
>我认为你最接近的就是使用IIf:
>I think the closest you''ll get to that is by using IIf:
但是,什么更具可读性?
Yes, however, what is more readable?
我当然会说完整版本比缩写的IIf版本更具可读性和可维护性。
就我个人而言,我通常使用IIf的唯一方法是将复数添加到状态
消息,例如:
\\\
MsgBox(" Downloaded"& msgcount&" message" ;& IIf(msgcount = 1,"",
" s")。ToString)
///
在其他情况下(也可以说也是这个),他们几乎总是让代码更难以阅读。
-
(O)enone
I''d certainly say the "full" version is more readable and maintainable than
the abbreviated IIf version.
Personally the only thing I normally use IIf for is adding plurals to status
message, for example:
\\\
MsgBox("Downloaded " & msgcount &" message" & IIf(msgcount=1, "",
"s").ToString)
///
In other situations (and arguably in this one too) they nearly always make
the code harder to read.
--
(O)enone
这篇关于VB.NET相当于C#运算符'??'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!