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

Why is a Nullable not a valid Custom Attribute Parameter when T is?

If I have an enum like this public enum Hungry { Somewhat, Very, CouldEatMySocks } and a custom attribute like this public class HungerAttribute : Attribute { public Hungry HungerLevel { get; set; } public Hungry?…
Mike Two
  • 44,935
  • 9
  • 80
  • 96
27
votes
6 answers

optional/null-able OUT parameter in C#

I have a method that has several overrides. In one of the more expanded overrides, I want to return an OUT parameter but not in my simpler overrides. For example: public bool IsPossible(string param1, int param2) public bool IsPossible(string…
Derek Hunziker
  • 12,996
  • 4
  • 57
  • 105
27
votes
2 answers

Implicit conversion to System.Double with a nullable struct via compiler generated locals: why is this failing?

Given the following, why does the InvalidCastException get thrown? I can't see why it should be outside of a bug (this is in x86; x64 crashes with a 0xC0000005 in clrjit.dll). class Program { static void Main(string[] args) { …
codekaizen
  • 26,990
  • 7
  • 84
  • 140
27
votes
3 answers

Determine if a generic param is a Nullable type

I have the following VB.NET function, for example: Public Function MyFunction (Of TData) (ByVal InParam As Integer) As TData End Sub How do I, in a function, determine if TData is a NULLable Type?
Chad
  • 23,658
  • 51
  • 191
  • 321
27
votes
2 answers

Is Nullable a "Predefined value type" - Or how does Equals() and == work here?

For my own implementation of an Equals() method, I want to check a bunch of internal fields. I do it like this: ... _myNullableInt == obj._myNullableInt && _myString == obj._myString && ... I would assume, that this compares the values, including…
Marcel
  • 15,039
  • 20
  • 92
  • 150
27
votes
6 answers

Why is there a questionmark on the private variable definition?

I am reading an article about the MVVP Pattern and how to implement it with WPF. In the source code there are multiple lines where I cannot figure out what the question marks in it stand for. private DateTime? _value; What does the ? mean in the…
Booser
  • 576
  • 2
  • 9
  • 25
26
votes
4 answers

Why Nullable is a struct?

I was wondering why Nullable is a value type, if it is designed to mimic the behavior of reference types? I understand things like GC pressure, but I don't feel convinced - if we want to have int acting like reference, we are probably OK with all…
NOtherDev
  • 9,542
  • 2
  • 36
  • 47
26
votes
7 answers

Nullable types in strongly-typed datatables/datasets - workarounds?

Strongly-typed DataTables support "nullable" field types, except that the designer will not allow you change the setting to "allow nulls" for any value type fields. (ie: String types allow nullable, but int's do not). The workaround is to call…
Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
25
votes
3 answers

Nullable Method Arguments in C#

Duplicate Question Passing null arguments to C# methods Can I do this in c# for .Net 2.0? public void myMethod(string astring, int? anint) { //some code in which I may have an int to work with //or I may not... } If not, is there something similar…
One Monkey
25
votes
1 answer

Not annotated parameter overrides @??? parameter

While overriding code: @Override public void open(ExecutionContext executionContext) { super.open(executionContext); from: org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader I received an IntelliJ…
DerBenniAusA
  • 727
  • 1
  • 7
  • 13
24
votes
3 answers

Cannot implicitly convert type bool?

I am trying to convert my nullable bool value and I am getting this error. Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?) For example: public virtual bool? MyBool { get; set; …
user603007
  • 11,416
  • 39
  • 104
  • 168
24
votes
2 answers

Passing null into a DataTable from a single line conditional statement parsing string values

I have an app that loops through a fixed width text file, reads each line into a string variable and uses the .Substring() method to find data for a given field. For a given field, it checks to see if the contents are simply spaces, or if there is…
Tom Miller
  • 536
  • 1
  • 4
  • 14
24
votes
2 answers

Why is it impossible to call static methods on Nullable shorthands?

I thought T? is just a compiler shorthand for Nullable. According to MSDN: The syntax T? is shorthand for Nullable, where T is a value type. The two forms are interchangeable. However, there is a little (insignificant) difference: Visual…
vojta
  • 5,591
  • 2
  • 24
  • 64
24
votes
4 answers

Best way to databind a Winforms control to a nullable type?

I'm currently using winforms databinding to wire up a data editing form. I'm using the netTiers framework through CodeSmith to generate my data objects. For database fields that allow nulls it creates nullable types. I've found that using…
Steve Hiner
  • 2,523
  • 3
  • 24
  • 33
24
votes
2 answers

Why are generic and non-generic structs treated differently when building expression that lifts operator == to nullable?

This looks like a bug in lifting to null of operands on generic structs. Consider the following dummy struct, that overrides operator==: struct MyStruct { private readonly int _value; public MyStruct(int val) { this._value = val; } …
sinelaw
  • 16,205
  • 3
  • 49
  • 80