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

Nullable object must have a value?

On the line: bool travel = fill.travel.Value; I am getting the following error: Nullable object must have a value and i am not sure why. All I want to do is get the value in the database of travel which is currently false. Any help would be…
Sealer_05
  • 5,346
  • 8
  • 35
  • 53
56
votes
2 answers

Nullable reference types: How to specify "T?" type without constraining to class or struct

I want to create a generic class that has a member of type T. T may be a class, a nullable class, a struct, or a nullable struct. So basically anything. This is a simplified example that shows my problem: #nullable enable class Box { public…
Andent
  • 601
  • 5
  • 5
53
votes
3 answers

Why does an implicit conversion operator from to accept ?

This is a weird behaviour that I cannot make sense of. In my example I have a class Sample and an implicit conversion operator from T to Sample. private class Sample { public readonly T Value; public Sample(T value) { Value…
Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
52
votes
9 answers

Is there any difference between type? and Nullable?

In C# are the nullable primitive types (i.e. bool?) just aliases for their corresponding Nullable type or is there a difference between the two?
MojoFilter
  • 12,256
  • 14
  • 53
  • 61
52
votes
6 answers

What is 'long?' data type?

I am going over some code written by another developer and am not sure what long? means: protected string AccountToLogin(long? id) { string loginName = ""; if (id.HasValue) { try {....
Ayush
  • 41,754
  • 51
  • 164
  • 239
51
votes
11 answers

Determine Oracle null == null

I wish to search a database table on a nullable column. Sometimes the value I'm search for is itself NULL. Since Null is equal to nothing, even NULL, saying where MYCOLUMN=SEARCHVALUE will fail. Right now I have to resort to where…
James Curran
  • 101,701
  • 37
  • 181
  • 258
51
votes
7 answers

Get short date for System Nullable datetime (datetime ?) in C#

How to get short date for Get short date for System Nullable datetime (datetime ?) for ed 12/31/2013 12:00:00 --> only should return 12/31/2013. I don't see the ToShortDateString available.
user2811143
  • 521
  • 1
  • 4
  • 4
51
votes
10 answers

How to exclude null properties when using XmlSerializer

I'm serializing a class like this public MyClass { public int? a { get; set; } public int? b { get; set; } public int? c { get; set; } } All of the types are nullable because I want minimal data stored when serializing an object of this…
Allen Rice
  • 19,068
  • 14
  • 83
  • 115
50
votes
8 answers

How can I fix this up to do generic conversion to Nullable?

I currently use this handy conversion extension method to do conversions between types: public static T To(this IConvertible obj) { return (T)Convert.ChangeType(obj, typeof(T)); } However, it doesn't like converting valid…
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
50
votes
5 answers

How to return when an optional is empty?

I love that optionals are in the Java standard library now. But there is one basic problem that I keep running into that I haven't figured out how to solve in the best way (easiest to read and understand, prettiest, shortest): How to return from a…
Lii
  • 11,553
  • 8
  • 64
  • 88
49
votes
7 answers

Idiomatic way to create a Stream from a Nullable object

What is the best/idiomatic way of performing a null-check before generating a stream? I have a method that receives a List that might be null. So I can't just call stream() on the value that is passed. Is there some static helper in that would give…
checketts
  • 14,167
  • 10
  • 53
  • 82
49
votes
6 answers

C# ADO.NET: nulls and DbNull -- is there more efficient syntax?

I've got a DateTime? that I'm trying to insert into a field using a DbParameter. I'm creating the parameter like so: DbParameter datePrm = updateStmt.CreateParameter(); datePrm.ParameterName = "@change_date"; And then I want to put the value of the…
Stewart Johnson
  • 14,281
  • 7
  • 61
  • 70
49
votes
3 answers

Why should I specify @Column( nullable = false )?

I have an entity annotated with @Entity. If I am responsible for creating the CREATE TABLE scripts why should I specify @Column( nullable = false ) when I can create a column in the database with the NOT NULL keywords? Is there any example that…
Fagner Brack
  • 2,365
  • 4
  • 33
  • 69
48
votes
4 answers

Nullable type is not a nullable type?

I was doing some testing with nullable types, and it didn't work quite as I expected: int? testInt = 0; Type nullableType = typeof(int?); Assert.AreEqual(nullableType, testInt.GetType()); // not the same type This doesn't work either: DateTime?…
Blake Pettersson
  • 8,927
  • 3
  • 27
  • 36
47
votes
5 answers

How to debug and fix 'Nullable object must have a value' within Entity Framework Core?

I'm doing a projection in this method: public async Task Get(int tradeId) { var firstOrDefaultAsync = await context.EvgTGTrade .Where(x => x.IdTrade == tradeId) .Select(trade => new TradeDetail { …
SuperJMN
  • 13,110
  • 16
  • 86
  • 185