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 which allows to include a value type, and can assert null :

?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/

spacer

2011-12-05 by liuyunsx | Categories: C# | Tags: c#, dotnet, null, type |


Related posts:

  1. Links and pointers.Part 2 (dotnet—c#)
  2. How to retrieve the default of a kind?
  3. How to verify that an object is of a certain type?
  4. How to create an instance of a type dynamically from a string?
  5. How to tell if a type implements a given interface?
  6. Enum Type (dotnet — C#)
  7. Compound assignment operators.Part 1 (dotnet — C#)
  8. Compound assignment operators.Part 2 (dotnet — C#)
  9. How to create an instance of a type dynamically?
  10. Arrays of arrays — (dotnet–c#)

Leave a Reply


gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.