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
89
votes
6 answers

How to set null value to int in c#?

int value=0; if (value == 0) { value = null; } How can I set value to null above? Any help will be appreciated.
user2902180
  • 1,021
  • 3
  • 12
  • 12
89
votes
2 answers

C# code won't compile. No implicit conversion between null and int

Possible Duplicate: Nullable types and the ternary operator: why is `? 10 : null` forbidden? Why doesn't this work? Seems like valid code. string cert = ddCovCert.SelectedValue; int? x = (string.IsNullOrEmpty(cert)) ? null: int.Parse(cert); …
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
88
votes
8 answers

Why can't I check if a 'DateTime' is 'Nothing'?

In VB.NET, is there a way to set a DateTime variable to "not set"? And why is it possible to set a DateTime to Nothing, but not possible to check if it is Nothing? For example: Dim d As DateTime = Nothing Dim boolNotSet As Boolean = d Is Nothing…
Muleskinner
  • 14,150
  • 19
  • 58
  • 79
86
votes
5 answers

Deserializing empty xml attribute value into nullable int property using XmlSerializer

I get an xml from the 3rd party and I need to deserialize it into C# object. This xml may contain attributes with value of integer type or empty value: attr=”11” or attr=””. I want to deserialize this attribute value into the property with type of…
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
85
votes
8 answers

How to check multiple objects for nullity?

Often, I can see a code constructs like following: if(a == null || b == null || c == null){ //... } I wonder if there is any widely used library (Google, Apache, etc.) to check against nullity for multiple objects at once, e.g.: if(anyIsNull(a,…
Krzysztof Wolny
  • 10,576
  • 4
  • 34
  • 46
84
votes
3 answers

Do short-circuiting operators || and && exist for nullable booleans? The RuntimeBinder sometimes thinks so

I read the C# Language Specification on the Conditional logical operators || and &&, also known as the short-circuiting logical operators. To me it seemed unclear if these existed for nullable booleans, i.e. the operand type Nullable (also…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
83
votes
3 answers

Query to check whether a column is nullable

Query to check whether a column is nullable (null values are allowed in the column or not). It should preferably return yes/no or 1/0 or true/false.
user646093
  • 1,495
  • 3
  • 15
  • 20
81
votes
5 answers

.NET - Convert Generic Collection to DataTable

I am trying to convert a generic collection (List) to a DataTable. I found the following code to help me do this: // Sorry about indentation public class CollectionHelper { private CollectionHelper() { } // this is the method I have been…
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
80
votes
4 answers

How to write nullable int in java?

I want to convert a web form to a model in Java. In C# I can write this: public class Test { public int? Id{get;set;} } The id can be null. But in Java when using struts2 it throws an exception: Method…
Dozer
  • 5,025
  • 11
  • 36
  • 52
79
votes
7 answers

What does "DateTime?" mean in C#?

I am reading a .NET book, and in one of the code examples there is a class definition with this field: private DateTime? startdate What does DateTime? mean?
Alvin S
  • 1,189
  • 1
  • 10
  • 16
78
votes
2 answers

Null aware function invocation operator

In the same way we can have nullableClassInstance?.method(blah) Is there a way to do nullableFunctionInstance?(blah) In other words, is there an operator that checks whether a function instance is not null, if so, invoke the function all in one…
xster
  • 6,269
  • 9
  • 55
  • 60
78
votes
8 answers

Why does >= return false when == returns true for null values?

I have two variables of type int? (or Nullable if you will). I wanted to do a greater-than-or-equal (>=) comparison on the two variables but as it turns out, this returns false if both variables are null, while obviously the == operator returns…
Koen
  • 3,626
  • 1
  • 34
  • 55
78
votes
8 answers

How to compare nullable types?

I have a few places where I need to compare 2 (nullable) values, to see if they're the same. I think there should be something in the framework to support this, but can't find anything, so instead have the following: public static bool…
David_001
  • 5,703
  • 4
  • 29
  • 55
77
votes
2 answers

Can an optional parameter be null in TypeScript?

According to this article, when strict null checking is enabled in TypeScript, you cannot assign null or undefined to a variable unless it is explicitly allowed with a union. // required value let req: string; req = "Something"; // OK req = null; …
Snackoverflow
  • 5,332
  • 7
  • 39
  • 69
74
votes
14 answers

Linq query with nullable sum

from i in Db.Items select new VotedItem { ItemId = i.ItemId, Points = (from v in Db.Votes where b.ItemId == v.ItemId select v.Points).Sum() } I got this query, however it fails if no votes are found with…
AndreasN
  • 2,881
  • 3
  • 24
  • 29