Algorithmischer Handel für .NET und Python✦86+ Konnektoren · Börsen · Broker · Krypto✦S#.Designer · Backtest · optimieren · live gehen✦Eine Community von 31,467+ Tradern und Entwicklern✦Algorithmischer Handel für .NET und Python✦86+ Konnektoren · Börsen · Broker · Krypto✦S#.Designer · Backtest · optimieren · live gehen✦Eine Community von 31,467+ Tradern und Entwicklern✦
Nullable types have the following characteristics:
Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)
The syntax T? is shorthand for Nullable(Of T), where T is a value type. The two forms are interchangeable.
Assign a value to a nullable type just as you would for an ordinary value type, for example int? x = 10; or double? d = 4.108. A nullable type can also be assigned the value null: int? x = null.
Use the Nullable(Of T).GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault();
Use the HasValue and Value read-only properties to test for null and retrieve the value, as shown in the following example: if(x.HasValue) j = x.Value;
The HasValue property returns true if the variable contains a value, or false if it is null.
The Value property returns a value if one is assigned. Otherwise, a System.InvalidOperationException is thrown.
The default value for HasValue is false. The Value property has no default value.
You can also use the == and != operators with a nullable type, as shown in the following example: if (x != null) y = x;
Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;
Nested nullable types are not allowed. The following line will not compile: Nullable<Nullable<int>> n;
Wir verwenden Cookies, um die beste Erfahrung auf unserer Website zu gewährleisten. Durch die weitere Nutzung unserer Seite stimmen Sie der Verwendung von Cookies zu.
Kommentare (4)
Kommentare zu diesem Thema sind gesperrt
нет
decimal не может принимать значение null, а decimal? - может
http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx?ppud=4
Nullable types have the following characteristics:
Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)
The syntax T? is shorthand for Nullable(Of T), where T is a value type. The two forms are interchangeable.
Assign a value to a nullable type just as you would for an ordinary value type, for example int? x = 10; or double? d = 4.108. A nullable type can also be assigned the value null: int? x = null.
Use the Nullable(Of T).GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault();
Use the HasValue and Value read-only properties to test for null and retrieve the value, as shown in the following example: if(x.HasValue) j = x.Value;
The HasValue property returns true if the variable contains a value, or false if it is null.
The Value property returns a value if one is assigned. Otherwise, a System.InvalidOperationException is thrown.
The default value for HasValue is false. The Value property has no default value.
You can also use the == and != operators with a nullable type, as shown in the following example: if (x != null) y = x;
Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;
Nested nullable types are not allowed. The following line will not compile: Nullable<Nullable<int>> n;
Спасибо!