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

Convert String to Nullable DateTime

Possible Duplicate: How do I use DateTime.TryParse with a Nullable? I have this line of code DateTime? dt = Condition == true ? (DateTime?)Convert.ToDateTime(stringDate) : null; Is this the correct way to convert string to Nullable…
Nalaka526
  • 11,278
  • 21
  • 82
  • 116
44
votes
7 answers

Compare nullable types in Linq to Sql

I have a Category entity which has a Nullable ParentId field. When the method below is executing and the categoryId is null, the result seems null however there are categories which has null ParentId value. What is the problem in here, what am I…
Ali Ersöz
  • 15,860
  • 11
  • 50
  • 64
44
votes
3 answers

Is there any difference between myNullableLong.HasValue and myNullableLong != null?

When I have a nullable long, for example, is there any difference between myNullableLong.HasValue and myNullableLong != null ... or is it just 'syntactic sugar'?
Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
44
votes
9 answers

Is there a more elegant way to add nullable ints?

I need to add numerous variables of type nullable int. I used the null coalescing operator to get it down to one variable per line, but I have a feeling there is a more concise way to do this, e.g. can't I chain these statements together somehow,…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
44
votes
1 answer

How to join MySQL tables using a nullable column?

I'm a little bit out of practice with MySQL, so I hope I can find some advice for my problem here. Basically I have two tables, call them A and B just for convenience. Both tables have a nullable column C of type varchar. When I join A and B using…
Per
  • 443
  • 1
  • 4
  • 4
43
votes
6 answers

Django: order by position ignoring NULL

I have a problem with Django queryset ordering. My model contains a field named position, a PositiveSmallIntegerField which I'd like to used to order query results. I use order_by('position'), which works great. Problem : my position field is…
user650108
  • 1,009
  • 1
  • 9
  • 18
43
votes
4 answers

Where in memory are nullable types stored?

This is maybe a follow up to question about nullable types. Where exactly are nullable value types (int?...) stored in memory? First I thought it's clear enough, as Nullable is struct and those are value types. Then I found Jon Skeet's article…
Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
43
votes
3 answers

Why is this code invalid in C#?

The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To…
mmattax
  • 27,172
  • 41
  • 116
  • 149
41
votes
6 answers

C# || operator not working with nullable booleans

I have the following piece of code in my LINQ: where (tf.Shipped || tf.Ordered || tf.Processed) Note that Shipped, Ordered and Processed are all nullable Boolean fields I am getting the following message: Operator || cannot be applied to…
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
41
votes
7 answers

Why shouldn't I always use nullable types in C#

I've been searching for some good guidance on this since the concept was introduced in .net 2.0. Why would I ever want to use non-nullable data types in c#? (A better question is why wouldn't I choose nullable types by default, and only use…
Matthew Vines
  • 27,253
  • 7
  • 76
  • 97
41
votes
10 answers

When should one use nullable types in c#?

I have been repeatedly asked the following questions in many interviews.... But still can't explain them with a simple example... What are nullable types in c#? When should one use nullable types in c#? Can you give a simple example? Any…
ACP
  • 34,682
  • 100
  • 231
  • 371
41
votes
6 answers

Serializing a Nullable in to XML

I am trying to serialize a class several of the data-members are Nullable objects, here is a example [XmlAttribute("AccountExpirationDate")] public Nullable AccountExpirationDate { get { return userPrincipal.AccountExpirationDate; } …
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
41
votes
8 answers

How to set null to a GUID property

I have an object of type Employee which has a Guid property. I know if I want to set to null I must to define my type property as nullable Nullable prop or Guid? prop. But in my case I'm not able to change the type of the prop, so it will…
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
41
votes
4 answers

Don't understand pre decrement operator behavior with Nullable type

Ok, this might be obvious for some of you but I am stumped with the behavior I'm getting from this rather simple code: public static void Main(string[] args) { int? n = 1; int i = 1; n = ++n - --i; Console.WriteLine("Without…
InBetween
  • 32,319
  • 3
  • 50
  • 90
40
votes
2 answers

Why are short null values converted to int null values for comparing with null?

When I compare nullable short values, the compiler converts them first to integer to make a compare with null. For example, consider this simple code: short? cTestA; if (cTestA == null) { ... } It is converted by the compiler to: short? CS$0$0001 =…
Aristos
  • 66,005
  • 16
  • 114
  • 150