How to set a null value to a value type?
A value type, by definition, can not be null : it necessarily a value.
However, there is a generic type Nullable
?View Code CSHARP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // integer not nullable int monInt1 = 42 ; // integer nullable nullable < int > monInt2 = null ; // integer nullable, scoring short int? monInt3 = null ; // Conversion implicit in int to nullable <int> monInt2 = monInt1 ; // Copy of an int to a Nullable <int> if (monInt2.HasValue) { // No implicit conversion from int to nullable <int>, use the Value property monInt1 = monInt2.Value; } |
Please indicate the source reference, the original address:
www.mydeveloperblog.com/dotnet/c-sharp/how-to-set-a-null-value-to-a-value-type/
2011-12-05 by liuyunsx | Categories: C# | Tags: c#, dotnet, null, type |
Related posts:
- Links and pointers.Part 2 (dotnet—c#)
- How to retrieve the default of a kind?
- How to verify that an object is of a certain type?
- How to create an instance of a type dynamically from a string?
- How to tell if a type implements a given interface?
- Enum Type (dotnet — C#)
- Compound assignment operators.Part 1 (dotnet — C#)
- Compound assignment operators.Part 2 (dotnet — C#)
- How to create an instance of a type dynamically?
- Arrays of arrays — (dotnet–c#)
Leave a Reply