Questions tagged [nullable]

The nullable tag is for issues relating to nullable members or types. A null is used to represent a missing or unknown value.

A data member is nullable if it can have the special value of NULL, instead of their common range of possible values. Nullable types are a feature of some statically-typed programming languages.

.NET

In , the built-in generic type System.Nullable<T> allows the programmer to use value types like int or DateTime with a value of null. Several .NET languages have a special syntax for declaring Nullable:

  • In , int? is an alias for System.Nullable<System.Int32>
  • In , Integer? is an alias for System.Nullable(Of System.Int32)

SQL

Null is a special marker used in SQL to indicate that data value do not exist in the database. Its proper use ensures various statistical aggregate functions perform correctly.

See more on Wikipedia.

D

The typecons module in package std has a templated struct - Nullable - for wrapping values types and allowing them to appear to have a null value (as in C#). Restriction is that the type must be able to be instantiated with T var or T var = T.init, where T represents the type being used.

Related tags

2417 questions
36
votes
4 answers

Nullable values in C++

I'm creating a database access layer in native C++, and I'm looking at ways to support NULL values. Here is what I have so far: class CNullValue { public: static CNullValue Null() { static CNullValue nv; return nv; …
DanDan
  • 10,462
  • 8
  • 53
  • 69
35
votes
6 answers

Cast object to decimal? (nullable decimal)

If have this in the setter of a property: decimal? temp = value as decimal?; value = "90" But after the cast, temp is null... What is the proper way to do this cast?
Natrium
  • 30,772
  • 17
  • 59
  • 73
35
votes
4 answers

Why does pattern matching on a nullable result in syntax errors?

I like to use pattern-matching on a nullable int i.e. int?: int t = 42; object tobj = t; if (tobj is int? i) { System.Console.WriteLine($"It is a nullable int of value {i}"); } However, this results in the following syntax errors: CS1003:…
Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70
35
votes
3 answers

Kotlin: eliminate nulls from a List (or other functional transformation)

Problem What is the idiomatic way of working around this limitation of the null-safety in the Kotlin type system? val strs1:List = listOf("hello", null, "world") // ERROR: Type Inference Failed: Expected Type Mismatch: // required:…
GlenPeterson
  • 4,866
  • 5
  • 41
  • 49
35
votes
3 answers

SparkSQL: How to deal with null values in user defined function?

Given Table 1 with one column "x" of type String. I want to create Table 2 with a column "y" that is an integer representation of the date strings given in "x". Essential is to keep null values in column "y". Table 1 (Dataframe df1): +----------+ | …
35
votes
4 answers

Add to Collection if Not Null

I have a very large object with many nullable-type variables. I also have a dictionary which I want to fill up with this object's non-null variables. The code will look something like this if (myObject.whatever !=…
Aabela
  • 1,408
  • 5
  • 19
  • 28
34
votes
3 answers

Which is better? cast nullable type or retrieve the value with Value property?

Is there a performance benefit to one way over the other? Are there other reasons to choose one over the other?
David W
  • 483
  • 4
  • 11
34
votes
4 answers

Can structs really not be null in C#?

Below is some code that demonstrates I cannot declare and initialize a struct type as null. The Nullable type is a struct, so why am I able to set it to null? Nullable b = null; if (b.HasValue) { Console.WriteLine("HasValue ==…
Nate
  • 5,237
  • 7
  • 42
  • 52
34
votes
4 answers

How to make a nullable field in a struct

I'm struggling to do table driven test, and I want do this: testCases := []struct { name string testUserID uint expected User // <- maybe nil expectedError error } Because the return values of tested function is…
Taichi
  • 2,297
  • 6
  • 25
  • 47
34
votes
5 answers

Whats the best way in kotlin for an null object's toString() method to return an empty string instead of "null"

According to the official kotlin documentation, the toString() call of a null object returns "null" toString() I want, that toString() should return an empty string ("") instead. I implemented it with an extension function. fun…
Lukas Lechner
  • 7,881
  • 7
  • 40
  • 53
34
votes
5 answers

Is it possible to use operator ?? and throw new Exception()?

I have a number of methods doing next: var result = command.ExecuteScalar() as Int32?; if(result.HasValue) { return result.Value; } else { throw new Exception(); // just an example, in my code I throw my own exception } I wish I could use…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
33
votes
13 answers

How to present the nullable primitive type int in Java?

I am designing an entity class which has a field named "documentYear", which might have unsigned integer values such as 1999, 2006, etc. Meanwhile, this field might also be "unknown", that is, not sure which year the document is created. Therefore,…
yinyueyouge
  • 3,684
  • 4
  • 25
  • 22
33
votes
1 answer

default(Nullable(type)) vs default(type)

In C#, is there a difference between default(Nullable) (or default(long?)) and default(long) ? Long is just an example, it can be any other struct type.
Eugene
  • 10,957
  • 20
  • 69
  • 97
33
votes
6 answers

Best way to represent Nullable member in C++?

Possible Duplicate: Nullable values in C++ What is the best way to represent nullable member in C++? In C#, we can use Nullable type. Such a data type is very much needed as not everything can have meaningful value. It is so important data…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
33
votes
2 answers

Why does C# require parentheses when using nullables in an expression?

I'm new to C# and while exploring the language features, I came across something strange: struct Foo { public Foo Identity() { return this; } public static void Bar(Foo? foo) { Foo foo1 = foo?.Identity().Value; // Does not…
Lukas92
  • 363
  • 2
  • 11