相关疑难解决方法(0)

为什么在VB.NET和C#中检查null值是否存在差异?

VB.NET中会发生这种情况:

Dim x As System.Nullable(Of Decimal) = Nothing
Dim y As System.Nullable(Of Decimal) = Nothing

y = 5
If x <> y Then
    Console.WriteLine("true")
Else
    Console.WriteLine("false") '' <-- I got this. Why?
End If
Run Code Online (Sandbox Code Playgroud)

但在C#中会发生这种情况:

decimal? x = default(decimal?);
decimal? y = default(decimal?);

y = 5;
if (x != y)
{
    Debug.WriteLine("true"); // <-- I got this -- I'm with you, C# :)
}
else
{
    Debug.WriteLine("false");
}
Run Code Online (Sandbox Code Playgroud)

为什么会有区别?

c# vb.net null .net-4.0

108
推荐指数
4
解决办法
5580
查看次数

C#中==运算符和Equals()方法的区别?

==Equals()示例有什么区别?我知道==用于比较运算符和Equals()方法用于比较string的内容.所以我试过

// first example
string s1 = "a";
string s2 = "a";
Console.Write(a.Equals(s2)); // returns true, but if I assign "b" to s2,
                             // then result will be false

// second example
string s1 ="a";
string s2 ="a";
Console.Write(s1 == s2);     // returns true
Run Code Online (Sandbox Code Playgroud)

怎么会这样?两者都是不同的对象引用.假设我们认为这些是参考.但我试着这样使用

string s1 = new string("ab");
string s2 = new string("ab");
Run Code Online (Sandbox Code Playgroud)

我收到编译时错误,无法将字符串转换为char

c# string equals equals-operator

17
推荐指数
2
解决办法
1万
查看次数

使用"Is"运算符选择Case

在VB.NET中,我必须比较一个select case语句中的一些对象.

由于默认情况下select case使用=operator而没有为对象定义,因此抛出了编译错误.

我目前使用此解决方法:

Select Case True
    Case sender Is StyleBoldButton

    Case sender Is StyleUnderButton

    Case sender Is StyleItalicButton

End Select
Run Code Online (Sandbox Code Playgroud)

这实际上有效.

是否有更漂亮的东西可以看到,更容易理解?

vb.net select case

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×2

vb.net ×2

.net-4.0 ×1

case ×1

equals ×1

equals-operator ×1

null ×1

select ×1

string ×1