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
581
votes
20 answers

How to convert C# nullable int to int

How do I convert a nullable int to an int? Suppose I have 2 type of int as below: int? v1; int v2; I want to assign v1's value to v2. v2 = v1; will cause an error. How do I convert v1 to v2?
KentZhou
  • 24,805
  • 41
  • 134
  • 200
527
votes
6 answers

What is the difference between Nullable.HasValue or Nullable != null?

I always used Nullable<>.HasValue because I liked the semantics. However, recently I was working on someone else's existing codebase where they used Nullable<> != null exclusively instead. Is there a reason to use one over the other, or is it purely…
lc.
  • 113,939
  • 20
  • 158
  • 187
468
votes
12 answers

How to declare a type as nullable in TypeScript?

I have an interface in TypeScript. interface Employee{ id: number; name: string; salary: number; } I would like to make salary as a nullable field (Like we can do in C#). Is this possible to do in TypeScript?
Amitabh
  • 59,111
  • 42
  • 110
  • 159
431
votes
1 answer

How to change a PG column to NULLABLE TRUE?

How can I accomplish this using Postgres? I've tried the code below but it doesn't work: ALTER TABLE mytable ALTER COLUMN mycolumn BIGINT NULL;
mateusmaso
  • 7,843
  • 6
  • 41
  • 54
362
votes
7 answers

Should Java 8 getters return optional type?

Optional type introduced in Java 8 is a new thing for many developers. Is a getter method returning Optional type in place of the classic Foo a good practice? Assume that the value can be null.
leonprou
  • 4,638
  • 3
  • 21
  • 27
350
votes
22 answers

How to parse a string into a nullable int

I'm wanting to parse a string into a nullable int in C#. ie. I want to get back either the int value of the string or null if it can't be parsed. I was kind of hoping that this would work int? val = stringVal as int?; But that won't work, so the…
Glenn Slaven
  • 33,720
  • 26
  • 113
  • 165
345
votes
10 answers

Performance surprise with "as" and nullable types

I'm just revising chapter 4 of C# in Depth which deals with nullable types, and I'm adding a section about using the "as" operator, which allows you to write: object o = ...; int? x = o as int?; if (x.HasValue) { ... // Use x.Value in here } I…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
318
votes
12 answers

Laravel Migration Change to Make a Column Nullable

I created a migration with unsigned user_id. How can I edit user_id in a new migration to also make it nullable()? Schema::create('throttle', function(Blueprint $table) { $table->increments('id'); // this needs to also be nullable, how…
user391986
  • 29,536
  • 39
  • 126
  • 205
287
votes
13 answers

Best way to check for nullable bool in a condition expression (if ...)

I was wondering what was the most clean and understandable syntax for doing condition checks on nullable bools. Is the following good or bad coding style? Is there a way to express the condition better/more cleanly? bool? nullableBool = true; if…
FireSnake
  • 2,983
  • 2
  • 16
  • 9
276
votes
21 answers

How can I format a nullable DateTime with ToString()?

How can I convert the nullable DateTime dt2 to a formatted string? DateTime dt = DateTime.Now; Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss")); //works DateTime? dt2 = DateTime.Now; Console.WriteLine(dt2.ToString("yyyy-MM-dd hh:mm:ss"));…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
263
votes
9 answers

Nullable types and the ternary operator: why is `? 10 : null` forbidden?

I just came across a weird error: private bool GetBoolValue() { //Do some logic and return true or false } Then, in another method, something like this: int? x = GetBoolValue() ? 10 : null; Simple, if the method returns true, assign 10 to the…
BFree
  • 102,548
  • 21
  • 159
  • 201
261
votes
10 answers

nullable object must have a value

There is paradox in the exception description: Nullable object must have a value (?!) This is the problem: I have a DateTimeExtended class, that has { DateTime? MyDataTime; int? otherdata; } and a…
Dani
  • 14,639
  • 11
  • 62
  • 110
253
votes
2 answers

Correct way to check if a type is Nullable

In order to check if a Type ( propertyType ) is nullable, I'm using: bool isNullable = "Nullable`1".Equals(propertyType.Name) Is there some way that avoid using magic strings ?
Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
235
votes
9 answers

How to set enum to null

I have an enum string name; public enum Color { Red, Green, Yellow } How to set it to NULL on load. name = ""; Color color = null; //error Edited: My bad, I didn't explain it properly. But all the answers related to nullable is perfect. My…
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112
232
votes
14 answers

How to check if an object is nullable?

How do I check if a given object is nullable in other words how to implement the following method... bool IsNullableValueType(object o) { ... } I am looking for nullable value types. I didn't have reference types in mind. //Note: This is just a…
Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
1
2 3
99 100