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

How to use .NET reflection to check for nullable reference type

C# 8.0 introduces nullable reference types. Here's a simple class with a nullable property: public class Foo { public String? Bar { get; set; } } Is there a way to check a class property uses a nullable reference type via reflection?
shadeglare
  • 7,006
  • 7
  • 47
  • 59
72
votes
13 answers

Optional return in C#.Net

Java 1.8 is receiving the Optional class, that allows us to explicitly say when a method may return a null value and "force" its consumer to verify if it is not null (isPresent()) before using it. I see C# has Nullable, that does something…
Hikari
  • 3,797
  • 12
  • 47
  • 77
72
votes
8 answers

Why does .ToString() on a null string cause a null error, when .ToString() works fine on a nullable int with null value?

selectedItem has two fields: int? _cost string _serialNumber In this example, _cost and _serialNumber of selectedItem are BOTH null. I am reading through the fields of selectedItem via their properties, and filling in textboxes with their values,…
CptSupermrkt
  • 6,844
  • 12
  • 56
  • 87
70
votes
3 answers

How to make a 'struct' Nullable by definition?

struct AccountInfo { String Username; String Password; } now if I want to have a Nullable instance I should write: Nullable myAccount = null; But I want make the struct Nullable by nature and it can be used like this (without…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
70
votes
2 answers

What are lifted operators?

I was looking at this article and am struggling to follow the VB.NET example that explains lifted operators. There doesn't seem to be an equivalent C# example or tutorial. I don't have much experience with operator overloading in general, so trying…
fletcher
  • 13,380
  • 9
  • 52
  • 69
69
votes
4 answers

DataSet does not support System.Nullable<> in Export

I was trying to generate a Report using Export to Excell, PDF, TextFile. Well I am doing this in MVC. I have a class which I named SPBatch (which is the exact name of my Stored Procedure in my SQL) and it contains the following: public string…
Ms. B
  • 1,073
  • 6
  • 15
  • 26
69
votes
4 answers

How will a C# switch statement's default label handle a nullable enum?

How will a C# switch statement's default label handle a nullable enum? Will the default label catch nulls and any unhandled cases?
Trey Mack
  • 4,215
  • 2
  • 25
  • 31
67
votes
7 answers

Check if Nullable Guid is empty in c#

Quoting from an answer from this question. Guid is a value type, so a variable of type Guid can't be null to start with. What then if I see this? public Nullable SomeProperty { get; set; } how should I check if this is null? Like…
Saturnix
  • 10,130
  • 17
  • 64
  • 120
66
votes
6 answers

Conditional operator assignment with Nullable types?

EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text) ? null : Convert.ToInt32(employeeNumberTextBox.Text), I often find myself wanting to do things like this (EmployeeNumber is a Nullable as it's a property on a…
Grank
  • 5,242
  • 7
  • 33
  • 36
65
votes
2 answers

Trying to understand ?. (null-conditional) operator in C#

I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { …
Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26
64
votes
5 answers

How can I convert decimal? to decimal

may be it is a simple question but I'm try all of conversion method! and it still has error! would you help me? decimal? (nullable decimal) to decimal
Negar
  • 866
  • 2
  • 10
  • 20
62
votes
9 answers

What's the difference between "bool" and "bool?"?

I use the "bool" type for variables as I was used to in C++, and I try to put the values of functions or properties I expect to be boolean into my variable. However I often encounter cases where the result type is "bool?" and not "bool" and the…
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
61
votes
3 answers

How much disk-space is needed to store a NULL value using postgresql DB?

let's say I have a column on my table defined the following: "MyColumn" smallint NULL Storing a value like 0, 1 or something else should need 2 bytes (1). But how much space is needed if I set "MyColumn" to NULL? Will it need 0 bytes? Are there…
Chris
  • 4,325
  • 11
  • 51
  • 70
60
votes
3 answers

Is there a way to use the default value on a non-optional parameter when null is passed?

For example, if I have the following data class: data class Data( val name: String = "", val number: Long = 0 ) And functions that can return null: fun newName(): String? {} fun newNumber(): Long? {} I know I can use the following to use…
Bryan
  • 14,756
  • 10
  • 70
  • 125
60
votes
2 answers

Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal

I think this question will bring me instant fame here on Stack Overflow. Suppose you have the following type: // represents a decimal number with at most two decimal places after the period struct NumberFixedPoint2 { decimal number; // an…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181