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
230
votes
4 answers

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and

Why does this not compile? int? number = true ? 5 : null; Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and
davidhq
  • 4,660
  • 6
  • 30
  • 40
206
votes
5 answers

How does comparison operator works with null int?

I am starting to learn nullable types and ran into following behavior. While trying nullable int, i see comparison operator gives me unexpected result. For example, In my code below, The output i get is "both and 1 are equal". Note, it does not…
Ron5504
  • 2,676
  • 2
  • 13
  • 15
203
votes
4 answers

In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them

If I have a nullable type Xyz?, I want to reference it or convert it to a non-nullable type Xyz. What is the idiomatic way of doing so in Kotlin? For example, this code is in error: val something: Xyz? = createPossiblyNullXyz() something.foo() //…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
201
votes
3 answers

Nullable return types in PHP7

PHP 7 introduces return type declarations. Which means I can now indicate the return value is a certain class, interface, array, callable or one of the newly hintable scalar types, as is possible for function parameters. function returnHello():…
Jeroen De Dauw
  • 10,321
  • 15
  • 56
  • 79
182
votes
5 answers

The type 'string' must be a non-nullable type in order to use it as parameter T in the generic type or method 'System.Nullable'

Why do I get Error "The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'"? using System; using System.Collections; using System.Collections.Generic; using…
MiscellaneousUser
  • 2,915
  • 4
  • 25
  • 44
178
votes
11 answers

How to use @Nullable and @Nonnull annotations more effectively?

I can see that @Nullable and @Nonnull annotations could be helpful in preventing NullPointerExceptions but they do not propagate very far. The effectiveness of these annotations drop off completely after one level of indirection, so if you only add…
Rylander
  • 19,449
  • 25
  • 93
  • 144
175
votes
4 answers

Difference between nullable, __nullable and _Nullable in Objective-C

With Xcode 6.3 there were new annotations introduced for better expressing the intention of API's in Objective-C (and to ensure better Swift support of course). Those annotations were of course nonnull, nullable and null_unspecified. But with Xcode…
Legoless
  • 10,942
  • 7
  • 48
  • 68
167
votes
10 answers

Convert nullable bool? to bool

How do you convert a nullable bool? to bool in C#? I have tried x.Value or x.HasValue ...
KentZhou
  • 24,805
  • 41
  • 134
  • 200
163
votes
10 answers

Can't find @Nullable inside javax.annotation.*

I want use @Nullable annotation to eliminate NullPointerExceptions. I found some tutorials on the net, I noticed that this annotation comes from the package javax.annotation.Nullable; but when I import it a compilation error is generated: cannot…
user2354035
160
votes
5 answers

Nullable type issue with ?: Conditional Operator

Could someone explain why this works in C#.NET 2.0: Nullable foo; if (true) foo = null; else foo = new DateTime(0); ...but this doesn't: Nullable foo; foo = true ? null : new DateTime(0); The…
Nick Gotch
  • 9,167
  • 14
  • 70
  • 97
150
votes
6 answers

Nullable ToString()

I see everywhere constructions like: int? myVar = null; string test = myVar.HasValue ? myVar.Value.ToString() : string.Empty; Why not use simply: string test = myVar.ToString(); Isn't that exactly the same ? At least Reflector says that: …
IamDeveloper
  • 5,156
  • 6
  • 34
  • 50
148
votes
6 answers

Are nullable types reference types?

When I declare an int as nullable int? i=null; Does i here become a reference type?
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163
146
votes
8 answers

C# generic type constraint for everything nullable

So I have this class: public class Foo where T : ??? { private T item; public bool IsNull() { return item == null; } } Now I am looking for a type constraint that allows me to use everything as type parameter that can…
jkammerer
  • 1,577
  • 2
  • 10
  • 6
143
votes
4 answers

c# why can't a nullable int be assigned null as a value

Explain why a nullable int can't be assigned the value of null e.g int? accom = (accomStr == "noval" ? null : Convert.ToInt32(accomStr)); What's wrong with that code?
mancmanomyst
  • 2,118
  • 6
  • 21
  • 23
142
votes
3 answers

What is the default value of the nullable type "int?" (including question mark)?

In C#, what is the default value of a class instance variable of type int?? For example, in the following code, what value will MyNullableInt have if it is never explicitly assigned? class MyClass { public int? MyNullableInt; } (It seems likely…
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170