开发者

Dim intResult As Integer = Nothing

开发者 https://www.devze.com 2023-01-29 01:48 出处:网络
VB.NET compiles: Dim intResult As Integer = Nothing C# does not: int intResult = null; // cannot convert

VB.NET compiles:

Dim intResult As Integer = Nothing

C# does not:

int intResult = null; // cannot convert

How the result finally goes to MSI开发者_JS百科L?

More that than, VB code is OK:

If intResult > Nothing Then

EDIT

OK, MS says:

Assigning Nothing to a variable sets it to the default value for its declared type.

But it tells nothing about Nothing comparation.


Nothing in VB.NET is actually equivalent to default(Type) in C#.

So int intResult = default(int); is the C# equivalent.

According to the VB.NET Language Reference: "Assigning Nothing to a variable sets it to the default value for its declared type. If that type contains variable members, they are all set to their default values."


Edit: Regarding the comparison to Nothing: I'm guessing intResult > Nothing is interpreted as intResult > 0, because the compile-time type of intResult is Integer, which has a default value of 0. If the type of intResult is not known at compile time (e.g., it is a boxed Integer), I suspect that this would not compile.

See the Community Content section at the bottom of the VB.NET Language Reference page for a similar example (Nothing < 2).


Ints are not nullable in c# simple as that. If you want to you can use System.Nullable<int> short for whinch is

int? result = null.

0

精彩评论

暂无评论...
验证码 换一张
取 消