Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / C#
Article

Set Value Types to Null

Rate me:
Please Sign up or sign in to vote.
1.62/5 (9 votes)
15 Nov 2006 30.6K   91   7   1
How to assign Null to Value Types

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


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Gloomholder1-Sep-09 23:05
Gloomholder1-Sep-09 23:05 

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

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