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
17
votes
2 answers

Why null statement ToString() returns an empty string?

I am just wondering what is the difference between the two next statements: Causes a NullReferenceException - it is OK. object test1 = default(int?); object result = test1.ToString(); Returns an empty string "", why? object test2 =…
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
17
votes
3 answers

Adding detectable Nullable values to CsvHelper

I was wondering if CsvHelper by Josh Close has anything in the configuration I am missing to translate values to null. I am a huge fan of this library, but I always thought there should be some sort of configuration to let it know what values…
Matt Sanders
  • 953
  • 1
  • 13
  • 23
17
votes
5 answers

Why can't nullables be declared const?

[TestClass] public class MsProjectIntegration { const int? projectID = null; // The type 'int?' cannot be declared const // ... } Why can't I have a const int?? Edit: The reason I wanted a nullable int as a const is because I'm just…
Shawn
  • 19,465
  • 20
  • 98
  • 152
17
votes
2 answers

Get PropertyType.Name in reflection from Nullable type

I want use reflection for get properties type. this is my code var properties = type.GetProperties(); foreach (var propertyInfo in properties) { model.ModelProperties.Add( new KeyValuePair
user1968030
17
votes
5 answers

Can I cast from DBNull to a Nullable Bool in one line?

I have a database query which will either return NULL or a boolean (bit) value. I wish to store this value in a variable of type Nullable in C#. I can't seem to find an acceptable mix of explict casts and conversions that do this in a simple…
Widor
  • 13,003
  • 7
  • 42
  • 64
16
votes
8 answers

Rewrite HasValue to the ?? Operators

Is it safe to rewrite the following code: bool b = foo.bar.HasValue ? foo.bar.Value : false; to bool b = foo.bar.Value ?? false; where bar is the nullable type bool?
radbyx
  • 9,352
  • 21
  • 84
  • 127
16
votes
1 answer

NHibernate 2.* mapping files: how to define nullable DateTime type (DateTime?)?

I know one of the breaking changes with NHibernate 2.* is that the NHibernate.Nullables are no longer supported. Therefore, what do you use in your mapping file to map the nullable DateTime? type? For i.e.: Understandably doesn't work:
Ted
  • 7,122
  • 9
  • 50
  • 76
16
votes
4 answers

Kotlin: equal comparison seems OK on nullable, but greater than comparison is not

I am new to Kotlin. I am following along a tutorial where the GUI portion involves this code snippet: sampleList.addMouseListener(object: MouseAdapter() { override fun mouseClicked(mouseEvent: MouseEvent?) { if…
Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
16
votes
1 answer

C# how to check for null. (value is null) or (null == value). Can we use `is` operator instead of == operator

C# how to check for null. (value is null) or (null == value). Can we use is operator instead of == operator? C# 7.0 supports const pattern with is operator. So we can use is null for all null checking ? Can the object be empty as well besides being…
Shahid Roofi Khan
  • 957
  • 1
  • 8
  • 19
16
votes
3 answers

Can Nullable be used as a functor in C#?

Consider the following code in C#. public int Foo(int a) { // ... } // in some other method int? x = 0; x = Foo(x); The last line will return a compilation error cannot convert from 'int?' to 'int' which is fair enough. However, for example…
Bobby
  • 785
  • 5
  • 9
16
votes
3 answers

Why is GetType() returning DateTime type for Nullable

Possible Duplicate: Nullable type is not a nullable type? In the following code: DateTime? dt = DateTime.Now; MessageBox.Show(dt.GetType().ToString()); the message box shows "System.DateTime", instead of Nullable. The following also…
veljkoz
  • 8,384
  • 8
  • 55
  • 91
16
votes
1 answer

Kotlin. How to check if the field is nullable via reflection?

I'm developing a code generator that takes the data from the classes during runtime. This generator is designed to work only with Kotlin. At the moment, I was faced with the problem, as I don't know how to check if the field is nullable. So the…
Yanislav Kornev
  • 283
  • 2
  • 6
16
votes
2 answers

Django filter a ForeignKey field when it is null

Let's say I have two tables in Django, TableA and TableB. Table A contains some boolean field, bool, and TableB contains a foreign key field, for_field to TableA, which can be Null. class TableA(models.Model): bool = models.BooleanField() class…
mathiascg
  • 550
  • 1
  • 5
  • 15
16
votes
1 answer

Does it look like a C# bug for you?

Create a console app to reproduce: struct Test { public static readonly Test? Null = null; } class Program { static void Main(string[] args) { var t = Test.Null; } } It is compilable, but we will have the following at run…
Dmitry Nogin
  • 3,670
  • 1
  • 19
  • 35
16
votes
2 answers

What is the default nullable constraint setting for a liquibase column?

I'm creating a new table, like this:
LimaNightHawk
  • 6,613
  • 3
  • 41
  • 60