Click here to Skip to main content
Click here to Skip to main content

Set Value Types to Null

, 15 Nov 2006
Rate this:
Please Sign up or sign in to vote.
How to assign Null to Value Types
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

I created this article to show developers that are moving to .Net 2.0 that you can now set value type fields to null. This feature was introduced in .Net 2.0. 

Here is a list of some of the Value Types in .Net.

Structs
Enumerations
Integral types
Floating-point types
decimal
bool
user defined structs

Example

<FONT size=2><P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>void</FONT><FONT size=2> Main(</FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2>[] args)</P><P>{</P><P></FONT><FONT color=#0000ff size=2>    bool</FONT><FONT size=2>? mynullBool = </FONT><FONT color=#0000ff size=2>null</FONT><FONT size=2>;</P><P></FONT><FONT color=#0000ff size=2>    int</FONT><FONT size=2>? mynullInt = </FONT><FONT color=#0000ff size=2>null</FONT><FONT size=2>;</P><P></FONT><FONT color=#008000 size=2>   </FONT></P><P><FONT color=#008000 size=2>    // Note that mynullBool will be set to null</P></FONT><FONT size=2><P></FONT><FONT color=#008080 size=2>    Console</FONT><FONT size=2>.WriteLine(</FONT><FONT color=#800000 size=2>"mynullBool={0}"</FONT><FONT size=2>, mynullBool);</P><P></FONT><FONT color=#008000 size=2>    // Note that mynullInt will be set to null</P></FONT><FONT size=2><P></FONT><FONT color=#008080 size=2>    Console</FONT><FONT size=2>.WriteLine(</FONT><FONT color=#800000 size=2>"mynullInt={0}"</FONT><FONT size=2>, mynullInt);</P><P></FONT><FONT color=#008000 size=2>   </FONT></P><P><FONT color=#008000 size=2>    // How to Assign default values for null values</P></FONT><FONT size=2><P>    mynullBool = GetBool() ?? </FONT><FONT color=#0000ff size=2>true</FONT><FONT size=2>;</P><P>    mynullInt = GetInt() ?? 2006;</P><P></FONT><FONT color=#008000 size=2>    // Note that mynullBool will be set to true</P></FONT><FONT size=2><P></FONT><FONT color=#008080 size=2>    Console</FONT><FONT size=2>.WriteLine(</FONT><FONT color=#800000 size=2>"mynullBool={0}"</FONT><FONT size=2>, mynullBool);</P><P></FONT><FONT color=#008000 size=2>   // Note that mynullInt will be set to 2006</P></FONT><FONT size=2><P></FONT><FONT color=#008080 size=2>   Console</FONT><FONT size=2>.WriteLine(</FONT><FONT color=#800000 size=2>"mynullInt={0}"</FONT><FONT size=2>, mynullInt);</P><P>}</P><P> </P><P></FONT><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>bool</FONT><FONT size=2>? GetBool()</P><P>{</P><P></FONT><FONT color=#0000ff size=2>    return</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>null</FONT><FONT size=2>;</P><P>}</P><P></FONT><FONT color=#0000ff size=2></FONT> </P><P><FONT color=#0000ff size=2>static</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>? GetInt()</P><P>{</P><P></FONT><FONT color=#0000ff size=2>    return</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>null</FONT><FONT size=2>;</P><P>}</P></FONT>

Conclusion

The following link points to MSDN for value types. http://msdn2.microsoft.com/en-gb/library/s1ax56ch.aspx

The following link points to MSDN for nullable types. http://msdn2.microsoft.com/en-us/library/1t3y8s4s.aspx

About BuddyWork

Developed Enterprise Applications for various sectors in C, C++, C#, VB, VB.NET, Clipper, Artemis and many more languages. 

Currently working in London as a .Net Architect. 

Microsoft qualification consists of MCSD.Net and MCPD (Enterprise)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

BuddyWork

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberGloomholder2-Sep-09 0:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 15 Nov 2006
Article Copyright 2006 by BuddyWork
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid