Starting with Whidbey, C# supports nullable types (
value types that can also
null) and it does this using the
class defined in the mscorlib
assembly. It also provides a
which corresponds to
. Take a look at the following C# code snippet :-
int? q1 = null;
q1 = 99;
int? q2 = q1;
if (q2 != null)
q2 = null;
Now, take a look at the corresponding C++ code (C# code in comments) :-
q1 = Nullable<int>(99);
q1 = Nullable<int>(q1.Value + 1);
Nullable<int> q2 = q1;
if( q2.HasValue )
q2 = Nullable<int>();
Not very convenient, is it? Now look at this C++ code :-
q1 = 99;
CNullable<int> q2 = q1;
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
class for C++/CLI.
template<typename T> ref class CodeProject::Extra::CNullable sealed :
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
!= operators both of which have been overloaded.
The default constructor creates a
CNullable object assigned to
This overload handles the case where a
is passed to the constructor. If the pointer passed is not a
CNullable(const T% t)
object using the passed in template-argument type.
CNullable(const CNullable% n)
Converts to type T.
Note - If the object is currently nulled, an
int y = x4;
void operator =(void* p)
Assignment operator that handles the
Note - If
is a non-null pointer, an
x3 = nullptr;
const T operator =(const T% t)
Assignment operator for type T.
x4 = x3 = 13;
The underlying type is exposed by overloading the dereference operator.
x5 = (*x4)++;
bool operator ==(void* p)
if( x5 == nullptr )
bool operator !=(void* p)
if( x5 != nullptr )
Gives the string representation of the type. Internally calls the
Console::WriteLine( (String^)x5 );
virtual String^ ToString() override
Returns an empty string if the object is currently nulled, else calls
on the underlying template-argument type member and returns
Console::WriteLine( x5.ToString() );
Creates a new
Nullable<T> object (useful to
interact with .NET code that explicitly expects a
Normally you'd be able to pass a
directly since it implements
Nullable<int> n1 = x5.CreateNullable();
static CNullable^ FromNullable(Nullable<T> n)
Static method to create a new
CNullable<int> x6 = CNullable<int>::FromNullable(n1);
property virtual bool HasValue::get
Indicates whether the value is valid or whether it's a null-value.
property virtual Object^ Value::get
true, it returns the value of
the internal template argument type member, otherwise throws an
- May 18th, 2005 : Article first published on The Code Project.
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.
Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.
: If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish