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
40
votes
8 answers

How to specify null prop type in ReactJS?

I've got a prop on a ReactJS Component that's either null or an Immutable Map. At the bottom of my widget if I write: MyComponent.propTypes = { myMap: React.PropTypes.instanceOf(Immutable.Map) }; I am leaving this open to the possibility of…
user1261710
  • 2,539
  • 5
  • 41
  • 72
40
votes
8 answers

C# DBNull and nullable Types - cleanest form of conversion

I have a DataTable, which has a number of columns. Some of those columns are nullable. DataTable dt; // Value set. DataRow dr; // Value set. // dr["A"] is populated from T-SQL column defined as: int NULL What, then, is the cleanest form of…
user111013
39
votes
3 answers

Why does a `null` Nullable have a hash code?

Bit of a weird question... But can anyone give me a justification for why this would be expected behaviour? This just seems totally odd to me.... //Makes perfect sense object o = null; o.GetHashCode().Dump(); NullReferenceException: Object…
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
39
votes
3 answers

How is the boxing/unboxing behavior of Nullable possible?

Something just occurred to me earlier today that has got me scratching my head. Any variable of type Nullable can be assigned to null. For instance: int? i = null; At first I couldn't see how this would be possible without somehow defining an…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
39
votes
5 answers

Can I make an embedded Hibernate entity non-nullable?

What I want: @Embedded(nullable = false) private Direito direito; However, as you know there's no such attribute to @Embeddable. Is there a correct way to do this? I don't want workarounds.
André Chalella
  • 13,788
  • 10
  • 54
  • 62
38
votes
7 answers

In Kotlin How Can I Convert an Int? to an Int

I'm using a HashMap in Kotlin and when I get out of it the return type is Int?. How can I convert the Int? to an Int? So far I've tried using Int?.toInt(), but that seems to be returning an Int?. I'm writing a Fibonacci function, and my…
jjnguy
  • 136,852
  • 53
  • 295
  • 323
37
votes
6 answers

Should you assert not null with the assert statement in production code?

I've seen this question but have a few more questions about the usage of the assert keyword. I was debating with a few other coders about using assert. For this use case, there was a method that can return null if certain prerequisites are met. The…
Big_Bad_E
  • 947
  • 1
  • 12
  • 23
37
votes
9 answers

Detecting a Nullable Type via reflection

Surprisingly the following code fails the Assert: int? wtf = 0; Assert.IsType>(wtf); So just out curiosity, how can you determine if a given instance is a Nullable<> object or not?
justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
37
votes
2 answers

TryGetValue pattern with C# 8 nullable reference types

I'm playing with porting some code to C# to enable nullable reference types, and I've encountered some functions in our code that use the TryGetValue pattern. That is, something like this: public bool TryGetSession(string key, out Session session)…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
37
votes
4 answers

C# struct, how to assign a null value?

I have a list: List For some reason, it is not allowing me to assign null values to it. What do I do if I want to have no struct associated?
Andy Hin
  • 30,345
  • 42
  • 99
  • 142
37
votes
1 answer

C# 4: Dynamic and Nullable<>

So I've got some code that passes around this anonymous object between methods: var promo = new { Text = promo.Value, StartDate = (startDate == null) ? new Nullable() : new…
JustWorldFallacy
  • 371
  • 1
  • 3
  • 3
37
votes
4 answers

How does GetValueOrDefault work?

I'm responsible for a LINQ provider which performs some runtime evaluation of C# code. As an example: int? thing = null; accessor.Product.Where(p => p.anInt == thing.GetValueOrDefault(-1)) Currently the above code doesn't work with my LINQ provider…
Ian Newson
  • 7,679
  • 2
  • 47
  • 80
36
votes
6 answers

C# compiler throws Language Version (LangVersion) reference error "Invalid 'nullable' value: 'Enable' for C# 7.3"

I have solution with couple .NET Standard projects in all I wanted to enable c# 8 and nullable like below: netstandard2.1 8.0 enable
dnf
  • 1,659
  • 2
  • 16
  • 29
36
votes
6 answers

Why do nullable bools not allow if(nullable) but do allow if(nullable == true)?

This code compiles: private static void Main(string[] args) { bool? fred = true; if (fred == true) Console.WriteLine("fred is true"); else if (fred == false) Console.WriteLine("fred is false"); else…
Quibblesome
  • 25,225
  • 10
  • 61
  • 100
36
votes
2 answers

XmlSerializer and nullable attributes

I have a class with numerous Nullable properties which I want to be serializable to XML as attributes. This is apparently a no-no as they are considered 'complex types'. So, instead I implement the *Specified pattern, where I create an addition…
Barg
  • 2,968
  • 7
  • 26
  • 28