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
22
votes
6 answers

Reference types vs Nullable types ToString()

Could someone please be kind enough to explain why calling ToString() on an empty reference type causes an exception (which in my mind makes perfect sense, you cant invoke a method on nothing!) but calling ToString() on an empty Nullable(Of T)…
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
21
votes
2 answers

Argument order for '==' with Nullable

The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, ==. (The type of IsInitialized is bool). Using C# 7.1 and .NET 4.7. static void A(ISupportInitialize x) { if ((x as…
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
21
votes
2 answers

how are nullable types implemented under the hood in .net?

In our own Jon Skeet's C# in depth, he discusses the 3 ways to simulate a 'null' for value types: Magic value (e.g. earliest possible DateTime is taken to be 'null') Reference type wrapper boolean flag It is mentioned that nullable types use the…
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
21
votes
2 answers

Convert decimal? to double?

I am wondering what would be the best way (in the sense of safer and succinct) to convert from one nullable type to another "compatible" nullable type. Specifically, converting from decimal? to double? can be done using: public double?…
Camilo Martinez
  • 1,723
  • 2
  • 21
  • 26
20
votes
6 answers

How to get nullable DateTime out of the database

My SQL Server database contains nullable DateTime values. How can I convert them to a nullable DateTime object in my application in C#? This is what I would think it would look like, but it doesn't: DateTime? dt = (DateTime?) sqldatareader[0];
pikachu
  • 1,031
  • 2
  • 11
  • 20
20
votes
2 answers

TryParse to a nullable type

I would like to try to parse a string as a DateTime?, and if it fails then set the value to null. The only way I can think to do this is the following, but it doesn't seem very neat. DateTime temp; DateTime? whatIActuallyWant = null; if…
James
  • 7,343
  • 9
  • 46
  • 82
20
votes
6 answers

Why can the as operator be used with Nullable?

According to the documentation of the as operator, as "is used to perform certain types of conversions between compatible reference types". Since Nullable is actually a value type, I would expect as not to work with it. However, this code compiles…
recursive
  • 83,943
  • 34
  • 151
  • 241
20
votes
3 answers

Make a column nullable in DB2 when Data Capture is enabled

I'm using db2 version 9.7* and it seems impossible to make a NOT NULL column nullable in any straightforward way. Unfortunately the solution of using a more developer friendly database is not available. Basically, in MySQL speak, I want to do…
lukewm
  • 21,433
  • 6
  • 26
  • 28
20
votes
2 answers

What's the difference between Objects.requireNonNullElse() and Optional.ofNullable().orElse()?

Java 9 introduces the requireNonNullElse and requireNonNullElseGet methods to the Objects class. Are these functionally any different to the Optional.ofNullable() orElse() and orElseGet() methods? String foo = null; Objects.requireNonNullElse(foo,…
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
20
votes
2 answers

Where to put @Nullable on methods with nullable return types?

When using javax.annotation.Nullable to mark methods as "potentially returning null value", where to put the @Nullable annotation? On the method, or return type? Is there any technical difference, or is this strictly a style issue? Style…
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
20
votes
1 answer

Why does Nullable not match as a reference type for generic constraints

Possible Duplicate: Nullable type as a generic parameter possible? I came across a very weird thing with generic type constraints. I have a class like this: public SomeClass where T:class { } However, I've found I can't use nullable types as…
Earlz
  • 62,085
  • 98
  • 303
  • 499
20
votes
1 answer

The += operator with nullable types in C#

In C#, if I write int? x = null; x += x ?? 1 I would expect this to be equivalent to: int? x = null; x = x + x ?? 1 And thus in the first example, x would contain 1 as in the second example. But it doesn't, it contains null. The += operator…
Zac
  • 1,722
  • 1
  • 19
  • 22
19
votes
3 answers

Is there a nice way to simulate a "Maybe" or "option" type in Go?

I'm sending a reply to a request over a chan X, where X is a struct. The request is a search operation, so ideally I'd like to be able to return either an X, or report it wasn't found. This would be a task for a Maybe X in Haskell or an x option in…
Asherah
  • 18,948
  • 5
  • 53
  • 72
19
votes
1 answer

@OneToOne(optional=false) and @JoinColumn(nullable=false) used together

I've bumped into this example in JPA 2.0 FR Specification, 11.1.37. OneToOne Annotation, page 403: @OneToOne(optional=false) @JoinColumn(name="CUSTREC_ID", unique=true, nullable=false, updatable=false) public CustomerRecord getCustomerRecord() {…
Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
19
votes
3 answers

"Save changes is not permitted" when changing an existing column to be nullable

I've got a SQL Database Table, which has 35 existing records. One of the fields in this table is called Name, nvarchar(100), not null However, due to a recent change, I need to make this column nullable. When I change the column to allow nulls in…
Curtis
  • 101,612
  • 66
  • 270
  • 352