在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)
为什么会有区别?
==和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
在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)
这实际上有效.
是否有更漂亮的东西可以看到,更容易理解?