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

Is there a convenient way to filter a sequence of C# 8.0 nullable references, retaining only non-nulls?

I have code like this: IEnumerable items = new [] { "test", null, "this" }; var nonNullItems = items.Where(item => item != null); //inferred as IEnumerable var lengths = nonNullItems.Select(item => item.Length); //nullability…
Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
19
votes
6 answers

Is using Optional.ofNullable as a replacement for the ternary operator a good practice?

Consider the usage of this expression: String hi = Optional.ofNullable(sayHi()).orElse("-"); which effectively corresponds to this ternary expression: String hi = sayHi() != null ? sayHi() : "-"; Is this usage of Optional.ofNullable with a method…
gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
19
votes
2 answers

Filtering avoiding unexpected null using lambda's inline

I have list for each element I would like to do this (using Java 8): disabledUsersOnLDAP.stream().forEach(user -> usersRepository .findEnabledByUsername(user.getUserName()).setEnabled(false)); How ever…
rayman
  • 20,786
  • 45
  • 148
  • 246
19
votes
15 answers

What is the use of Nullable type?

a bool variable could hold true or false, while bool? could be null as well. Why would we need a third value for bool ? If it is not true, what ever it is, it is == false Can you suggest a scenario where I would fancy a bool? instead. Thanks
Asad
  • 21,468
  • 17
  • 69
  • 94
19
votes
2 answers

Reflection - check all nullable properties have values

I have to loop through all the properties in a few classes and check any nullable properties to see if they have a value. How do I cast the value returned from propertyInfo.GetValue() to a generic nullable type so that I can check the HasValue…
Paddy
  • 33,309
  • 15
  • 79
  • 114
19
votes
6 answers

Is a null value in .NET DateTime guaranteed to be less than a real value?

Perhaps my Google-Fu has failed me, but I haven't been able to determine if comparing a nullable in .NET will always be less than something else. I've got some code similar to this MyClass findLatest(List items){ DateTime? latest_tstamp…
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
18
votes
4 answers

How to Convert a String to a Nullable Type Which is Determined at Runtime?

I have the below code and I'd need to convert a string to a type which is also specified from String: Type t = Type.GetType("System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"); …
The Light
  • 26,341
  • 62
  • 176
  • 258
18
votes
5 answers

Returning nullable string types

So I have something like this public string? SessionValue(string key) { if (HttpContext.Current.Session[key].ToString() == null || HttpContext.Current.Session[key].ToString() == "") return null; return…
sarsnake
  • 26,667
  • 58
  • 180
  • 286
18
votes
5 answers

Whats the use of Nullable.GetUnderlyingType, if typeof(int?) is an Int32?

why is typeof int? an Int32 int? x = 1; Console.WriteLine(x.GetType().Name); If it is okay then what's the use of Nullable.GetUnderlyingType?
Mubashir Khan
  • 1,464
  • 1
  • 12
  • 17
18
votes
3 answers

How to Type Cast null as Bool in C#?

I'm having one null-able bool (bool?) variable, it holds a value null. One more variable of type pure bool, I tried to convert the null-able bool to bool. But I faced an error "Nullable object must have a value." My C# Code is bool? x = (bool?)…
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
18
votes
2 answers

A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'T'

I am getting the Error: A value of type '' cannot be used as a default parameter because there are no standard conversions to type 'T' while trying to write this piece of code protected T GetValue(Expression> property, T…
Mohit S
  • 13,723
  • 6
  • 34
  • 69
18
votes
1 answer

Is the C# compiler optimizing nullable types?

Can anybody shed any light on why this unit test is failing in Visual Studio 2013? [TestMethod] public void Inconceivable() { int? x = 0; Assert.AreEqual(typeof(int?), x.GetType()); }
Doug Clutter
  • 3,646
  • 2
  • 29
  • 31
17
votes
2 answers

Creating a nullable extension method ,how do you do it?

I have a situation where I need to compare nullable types. Suppose you have 2 values: int? foo=null; int? bar=4; This will not work: if(foo>bar) The following works but obviously not for nullable as we restrict it to value types: public static…
user9969
  • 15,632
  • 39
  • 107
  • 175
17
votes
5 answers

Why am I allowed to compare a non-nullable type with null?

Possible Duplicate: C# okay with comparing value types to null If I try to assign null to a non-nullable type in C#: System.DateTime time = null; I'll get a compile-time error: error CS0037: Cannot convert null to 'System.DateTime' because it…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
17
votes
3 answers

Why is django admin not accepting Nullable foreign keys?

Here is a simplified version of one of my models: class ImportRule(models.Model): feed = models.ForeignKey(Feed) name = models.CharField(max_length=255) feed_provider_category = models.ForeignKey(FeedProviderCategory, null=True) …
p.g.l.hall
  • 1,961
  • 2
  • 15
  • 26