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
33
votes
1 answer

What is the justification for this Nullable behavior with implicit conversion operators

I encountered some interesting behavior in the interaction between Nullable and implicit conversions. I found that providing an implicit conversion for a reference type from a value type it permits the Nullable type to be passed to a function…
Thomas
  • 3,603
  • 1
  • 18
  • 23
32
votes
3 answers

How could I return a nullable value in typescript

in an NPM module I use typescript "devDependencies": { "@types/node": "^8.0.0", "typescript": "^2.8.1" } and I want to return a private nullable parameter using a public method. Please refer to the axample below. The error I see…
jstolz
  • 431
  • 1
  • 4
  • 4
32
votes
3 answers

Why is the type of null + null implicitly String in Kotlin?

The following Kotlin code: val x = null + null results in x being of type String, which is correct as according to the docs for String.plus: Concatenates this string with the string representation of the given [other] object. If either the…
Morozov
  • 4,968
  • 6
  • 39
  • 70
32
votes
6 answers

In C# why can't a conditional operator implicitly cast to a nullable type

I am curious as to why an implicit cast fails in... int? someValue = SomeCondition ? ResultOfSomeCalc() : null; and why I have to perform an explicit cast instead int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null; It seems to me…
Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
31
votes
1 answer

How to check if a generic type parameter is nullable?

Possible Duplicate: Determine if a generic param is a Nullable type I'm trying to determine if a type parameter is Nullable. public T Get(int index) { var none=default(T); var t = typeof(T); BaseVariable v =…
Earlz
  • 62,085
  • 98
  • 303
  • 499
31
votes
10 answers

"Cannot implicitly convert type 'System.Guid?' to 'System.Guid'." - Nullable GUID

In my database, in one of the table I have a GUID column with allow nulls. I have a method with a Guid? parameter that inserts a new data row in the table. However when I say myNewRow.myGuidColumn = myGuid I get the following error: Cannot…
kjv
  • 11,047
  • 34
  • 101
  • 140
30
votes
15 answers

How liberal should I be with NOT NULL columns?

I'm designing a database schema, and I'm wondering what criteria I should use for deciding whether each column should be nullable or not. Should I mark as NOT NULL only those columns that absolutely must be filled out for a row to make any sense at…
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
30
votes
4 answers

Restricting a generic to things that can be null

I'd like to restrict a generic I'm coding to anything that can be null. That's basically any class + System.Nullable (e.g. int? and such). For the class part, it's rather easy: public class MyGeneric where T : class {} But then, this doesn't…
joce
  • 9,624
  • 19
  • 56
  • 74
30
votes
4 answers

When exactly do nullable types throw exceptions?

Consider the following code: int? x = null; Console.Write ("Hashcode: "); Console.WriteLine(x.GetHashCode()); Console.Write("Type: "); Console.WriteLine(x.GetType()); When executed, it writes that Hashcode is 0, but fails with…
30
votes
7 answers

Ternary operator VB vs C#: why resolves Nothing to zero?

I just shoot myself in the foot and would like to know whether there were actual reasons to make this situation possible. And anyway, this question can stay for the convenience of the future foot shooters. Suppose we have a nullable value in…
GSerg
  • 76,472
  • 17
  • 159
  • 346
29
votes
4 answers

Why GetType returns System.Int32 instead of Nullable?

Why is the output of this snippet System.Int32 instead of Nullable? int? x = 5; Console.WriteLine(x.GetType());
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
29
votes
3 answers

Does the "?." operator do anything else apart from checking for null?

As you might know, DateTime? does not have a parametrized ToString (for the purposes of formatting the output), and doing something like DateTime? dt = DateTime.Now; string x; if(dt != null) x = dt.ToString("dd/MM/yyyy"); will throw No…
iuliu.net
  • 6,666
  • 6
  • 46
  • 69
29
votes
3 answers

How does @RequestParam in Spring handle Guava's Optional?

@RequestMapping(value = "/contact.html", method = RequestMethod.POST) public final ModelAndView contact( @RequestParam(value = "name", required = false) Optional name) { How does Spring's @RequestMapping handle an Optional from…
Anders
  • 9,988
  • 7
  • 30
  • 36
28
votes
4 answers

Make Kotlin warn on assignment of flexible/platform type to non-null type?

When calling a non-nullability-annotated Java function from Kotlin, we get flexible-typed return values, denoted by exclamation marks, e.g. String!. Kotlin silently allows assigning these flexible values to a normal non-null type, e.g. String, which…
Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
28
votes
10 answers

Take the greater of two nullable values

Suppose I have two nullable integers: int? a = 10; int? b = 20; I want to take the biggest, non-null value, such that if both values are null, the result is null. I could write something long-winded such as: int? max; if (a == null) { max =…
Paul Turner
  • 38,949
  • 15
  • 102
  • 166