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
24
votes
4 answers

Cannot make @ManyToOne relationship nullable

I have a many-to-one relationship that I want to be nullable: @ManyToOne(optional = true) @JoinColumn(name = "customer_id", nullable = true) private Customer customer; Unfortunately, JPA keeps setting the column in my database as NOT NULL. Can…
vcattin
  • 687
  • 1
  • 4
  • 15
24
votes
9 answers

What is the memory footprint of a Nullable

An int (Int32) has a memory footprint of 4 bytes. But what is the memory footprint of: int? i = null; and : int? i = 3; Is this in general or type dependent?
PVitt
  • 11,500
  • 5
  • 51
  • 85
24
votes
4 answers

Is "int?" somehow a reference type?

What is the behind-the-scenes difference between int? and int data types? Is int? somehow a reference type?
Mike Comstock
  • 6,640
  • 10
  • 36
  • 41
23
votes
4 answers

C# cast object of type int to nullable enum

I just need to be able to cast an object to nullable enum. Object can be enum, null, or int. Thanks! public enum MyEnum { A, B } void Put(object value) { System.Nullable val = (System.Nullable)value; } Put(null); //…
dlsou
  • 635
  • 2
  • 8
  • 18
23
votes
2 answers

Linq OrderBy breaks with navigation property being null

Working with four tables. Users -> has basic user info including a userid and a departmentid (int) Groups -> basic group info including a groupid GroupsMembers -> table that has the relationship between a group and it's members, many to many…
Steph
  • 409
  • 2
  • 8
  • 14
23
votes
5 answers

Boxing / Unboxing Nullable Types - Why this implementation?

Extract from CLR via C# on Boxing / Unboxing value types ... On Boxing: If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it. In other words a Nullable < Int32 > with a value of 5 is boxed into a…
Preets
  • 6,792
  • 12
  • 37
  • 38
22
votes
2 answers

SqlParameter with Nullable value give error while ExecuteNonQuery?

I have an sql query that has a parameter that can be null in the database (Sql Server). The update method work fine until that user put a blank in the field, this produce a null value for the DataTime object (this object is nullable). The problem is…
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
22
votes
4 answers

Why cant I declare a generic list as nullable?

Im trying to use the following code: private Nullable> ipAddressRangeToBind; But I am getting the following warning: The type List must be a non-nullable value type in order to use it as a parameter 'T' in the generic type…
Exitos
  • 29,230
  • 38
  • 123
  • 178
22
votes
2 answers

Assigning null/Nullable to DateTime in Ternary Operation

I have a statement like DateTime ? dt = (string1 == string2) ? null; (DateTime)(txtbox.Text); which I cannot compile. Reason is : null cannot be assigned to DateTime. So, I have to declare a Nullable nullable variable and replace null…
iTSrAVIE
  • 846
  • 3
  • 12
  • 26
22
votes
5 answers

openapi springboot generator jackson no String-argument constructor/factory method to deserialize from String value

I'm using OpenApi SpringBoot generator to generate controller interfaces and models. This creates model classes with JsonNullable for nullable fields. However I'm getting a Jackson type definition error while POST request is sent with value…
22
votes
4 answers

Can I write a type guard that throws exceptions instead of returning booleans?

I have a class that uses the same type guard in multiple functions; something like this: function validData(d: Data | null): d is Data { return d !== null; } class C { data: Data | null; public doA() { if…
Clément
  • 12,299
  • 15
  • 75
  • 115
22
votes
2 answers

Can I write a type guard that asserts multiple invariants?

Can I write a type guard asserting something about one or multiple sub-objects of an argument? In pseudo-code, it might look like this: class C { a: number?; b: string?; function assertInitialized() : (this.a is number) and (this.b is…
Clément
  • 12,299
  • 15
  • 75
  • 115
22
votes
7 answers

why is null not equal to null false

I was reading this article: Get null == null in SQL And the consensus is that when trying to test equality between two (nullable) sql columns, the right approach is: where ((A=B) OR (A IS NULL AND B IS NULL)) When A and B are NULL, (A=B) still…
rouble
  • 16,364
  • 16
  • 107
  • 102
22
votes
6 answers

Coding practices for C# Nullable type

I have never used nullable types in my C# code. Now I have decided to change my coding practice by introducing nullable types in my code. What are the major changes in the coding practices should be made while making a transition from normal…
user366312
  • 16,949
  • 65
  • 235
  • 452
22
votes
3 answers

C# determine a Nullable property DateTime type when using reflection

I have a question on how to determine an object's Nullable property type. ObjectA has a property DateTime? CreateDate; When I iterate through its properties like the following code, how do I check if a property is a Nullable DateTime type? foreach…
Eatdoku
  • 6,569
  • 13
  • 63
  • 98