Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Nullable types in c++/cli?

Tags:

c++-cli

I have the following code, which I thought would work:

property Nullable<double> Angle {     Nullable<double> get() {         return nullptr;     } } 

It doesn't. How can I do it? Does c++/CLI even support nullable types?

like image 897
devoured elysium Avatar asked Sep 22 '09 01:09

devoured elysium


People also ask

How do you use nullable types?

You can declare nullable types using Nullable<t> where T is a type. Nullable<int> i = null; A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value.

How would you use nullable types in net?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false . However, in some applications a variable value can be undefined or missing.

What is the use of HasValue in C#?

If the HasValue property is true , the value of the current Nullable<T> object can be accessed with the Value property. Otherwise, attempting to access its value throws an InvalidOperationException exception.

What is the nullable type in C sharp?

The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.


1 Answers

OK, found it, after a lot of hassle:

to return null, just do

return Nullable<double>(); 

to return non-null:

return Nullable<double>(12321); 

It is important to declare the return value as Nullable<double> and not Nullable<double>^, as if you do it, when using other languages as C# and vb.net, you'll see the type as ValueType instead of double?.

like image 69
devoured elysium Avatar answered Oct 27 '22 02:10

devoured elysium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!