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

CNullable<T> : A convenient Nullable<T> equivalent class for C++/CLI

By , 18 May 2005
Rate this:
Please Sign up or sign in to vote.


Starting with Whidbey, C# supports nullable types (value types that can also be null) and it does this using the System::Nullable<T> generic class defined in the mscorlib assembly. It also provides a short-cut form, T? var which corresponds to System.Nullable<T> var. Take a look at the following C# code snippet :-

int? q1 = null;

if (q1.HasValue)
    Console.WriteLine("It's null");    

q1 = 99;

int? q2 = q1;
if (q2 != null)
    q2 = null;

Now, take a look at the corresponding C++ code (C# code in comments) :-

//int? q1 = null;
Nullable<int> q1;

//if (q1.HasValue)
//    Console.WriteLine(q1.Value);
//    Console.WriteLine("It's null");

    Console::WriteLine("It's null");

//q1 = 99;

q1 = Nullable<int>(99);


    q1 = Nullable<int>(q1.Value + 1);

//int? q2 = q1;

Nullable<int> q2 = q1;

//if (q2 != null)
//    q2 = null;

if( q2.HasValue )
    q2 = Nullable<int>();

Not very convenient, is it? Now look at this C++ code :-

//int? q1 = null;
CNullable<int> q1;

//if (q1.HasValue)
//    Console.WriteLine(q1.Value);
//    Console.WriteLine("It's null");

    Console::WriteLine("It's null");

//q1 = 99;

q1 = 99;



////int? q2 = q1;

CNullable<int> q2 = q1;

//if (q2 != null)
//    q2 = null;

if(q2 != nullptr)
    q2 = nullptr;

Well, that's a lot more closer to the C# code as far as ease-of-use is concerned, is it not? That's why I wrote my CNullable<> template ref class for C++/CLI.

Class Reference

template<typename T> ref class CodeProject::Extra::CNullable sealed : INullableValue

CNullable can be used to represent a value type (or simple native type) such that the type can be nulled. You can also compare it to nullptr using the == and != operators both of which have been overloaded.


  • CNullable()

    The default constructor creates a CNullable object assigned to nullptr.

    CNullable<char> x1;
  • CNullable(void* p)

    This overload handles the case where a nullptr is passed to the constructor. If the pointer passed is not a nullptr, an InvalidOperationException gets thrown.

    CNullable<double> x2(nullptr);
  • CNullable(const T% t)

    Constructs a CNullable object using the passed in template-argument type.

    CNullable<int> x3(100);
  • CNullable(const CNullable% n)

    Copy constructor.

    CNullable<int> x4(x3);


  • operator T()

    Converts to type T.

    Note - If the object is currently nulled, an InvalidOperationException is thrown.

    int y = x4;
  • void operator =(void* p)

    Assignment operator that handles the nullptr case.

    Note - If p is a non-null pointer, an InvalidOperationException is thrown.

    x3 = nullptr;
  • const T operator =(const T% t)

    Assignment operator for type T.

    x4 = x3 = 13;
  • T% operator*()

    The underlying type is exposed by overloading the dereference operator.

    x5 = (*x4)++;
  • bool operator ==(void* p)

    Allows == comparison with nullptr.

    if( x5 == nullptr )
  • bool operator !=(void* p)

    Allows != comparison with nullptr.

    if( x5 != nullptr )
  • operator String^()

    Gives the string representation of the type. Internally calls the ToString method.

    Console::WriteLine( (String^)x5 );


  • virtual String^ ToString() override

    Returns an empty string if the object is currently nulled, else calls ToString on the underlying template-argument type member and returns that string.

    Console::WriteLine( x5.ToString() );
  • Nullable<T> CreateNullable()

    Creates a new Nullable<T> object (useful to interact with .NET code that explicitly expects a Nullable<T>). Normally you'd be able to pass a CNullable object directly since it implements INullableValue.

    Nullable<int> n1 = x5.CreateNullable();
  • static CNullable^ FromNullable(Nullable<T> n)

    Static method to create a new CNullable object from a Nullable<T> object.

    CNullable<int> x6 = CNullable<int>::FromNullable(n1);


  • property virtual bool HasValue::get

    Implementation of INullableValue::HasValue. Indicates whether the value is valid or whether it's a null-value.

  • property virtual Object^ Value::get

    Implementation of INullableValue::Value. If HasValue is true, it returns the value of the internal template argument type member, otherwise throws an InvalidOperationException exception.



  • May 18th, 2005 : Article first published on The Code Project.


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

About the Author

Nish Sivakumar

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

QuestionCan you port it to C++? PinmemberCP Visitor18-May-05 2:11 
GeneralAnd if you're not using .Net... PinmemberStuart Dootson18-May-05 2:10 
GeneralRe: And if you're not using .Net... PinstaffNishant Sivakumar18-May-05 6:16 
Generalfixed link Pinmemberpeterchen14-Apr-11 9:33 

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 | Mobile
Web03 | 2.8.140421.2 | Last Updated 18 May 2005
Article Copyright 2005 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid