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
141
votes
3 answers

What does exclamation mark mean before invoking a method in C# 8.0?

I have found a code written in C# seemingly version 8.0. In the code, there is an exclamation mark before invoking a method. What does this part of the code mean, and above all, what are its uses? var foo = Entity!.DoSomething();
icn
  • 1,783
  • 2
  • 7
  • 13
137
votes
9 answers

How do I use DateTime.TryParse with a Nullable?

I want to use the DateTime.TryParse method to get the datetime value of a string into a Nullable. But when I try this: DateTime? d; bool success = DateTime.TryParse("some date text", out (DateTime)d); the compiler tells me 'out' argument is not…
Brian Sullivan
  • 27,513
  • 23
  • 77
  • 91
135
votes
5 answers

C# nullable string error

private string? typeOfContract { get { return (string?)ViewState["typeOfContract"]; } set { ViewState["typeOfContract"] = value; } } Later in the code I use it like this: typeOfContract = Request.QueryString["type"]; I am getting the following…
Mike Fielden
  • 10,055
  • 14
  • 59
  • 99
124
votes
3 answers

Set value to null in WPF binding

Please take a look at the following line This Price property from above is a Decimal? (Nullable decimal). I want that if user deletes the content of the textbox (i.e. enters an empty string), it should…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
123
votes
20 answers

C# elegant way to check if a property's property is null

In C#, say that you want to pull a value off of PropertyC in this example and ObjectA, PropertyA and PropertyB can all be null. ObjectA.PropertyA.PropertyB.PropertyC How can I get PropertyC safely with the least amount of code? Right now I would…
Jon Kragh
  • 4,529
  • 5
  • 26
  • 26
118
votes
6 answers

How to use nonnull and nullable Objective-C keywords in block-based API method

Consider the following method - (void)methodWithArg:(NSString *)arg1 andArg:(NSString *)arg2 completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler; With the new nonnull and nullable annotation keywords we can enrich it as…
albertodebortoli
  • 1,768
  • 2
  • 16
  • 20
113
votes
19 answers

Checkbox for nullable boolean

My model has a boolean that has to be nullable public bool? Foo { get; set; } so in my Razor cshtml I have @Html.CheckBoxFor(m => m.Foo) except that doesn't work. Neither does casting it with (bool). If I do @Html.CheckBoxFor(m =>…
DMulligan
  • 8,993
  • 6
  • 33
  • 34
102
votes
5 answers

Checking if Type instance is a nullable enum in C#

How do i check if a Type is a nullable enum in C# something like Type t = GetMyType(); bool isEnum = t.IsEnum; //Type member bool isNullableEnum = t.IsNullableEnum(); How to implement this extension method?
adrin
  • 3,738
  • 8
  • 40
  • 60
101
votes
3 answers

Invalid cast from 'System.Int32' to 'System.Nullable`1[[System.Int32, mscorlib]]

Type t = typeof(int?); //will get this dynamically object val = 5; //will get this dynamically object nVal = Convert.ChangeType(val, t);//getting exception here I am getting InvalidCastException in above code. For above I could simply write int?…
Brij
  • 11,731
  • 22
  • 78
  • 116
101
votes
4 answers

Nullable property to entity field, Entity Framework through Code First

Using the data annotation Required like so: [Required] public int somefield {get; set;} Will set somefield to Not Null in database, How can I set somefield to allow NULLs?, I tried setting it through SQL Server Management Studio but Entity…
chrisramon
  • 1,253
  • 2
  • 8
  • 8
99
votes
3 answers

Why doesn't incrementing Nullable throw an exception?

Could you please explain, why does Console.WriteLine write empty line (Console.WriteLine(null) give me compilation error) and why there isn't NullReferenceException (even a+=1 shouldn't raise it)? int? a = null; a++; // Why there is not…
Maxim Zhukov
  • 10,060
  • 5
  • 44
  • 88
98
votes
8 answers

Java check if boolean is null

How do you check if a boolean is null or not? So if I know "hideInNav" is null. How do I stop it from further executing? Something like the below doesn't seem to work but why? boolean hideInNav = parent.getProperties().get("hideInNav",…
Delmon Young
  • 2,013
  • 5
  • 39
  • 51
96
votes
7 answers

What's the difference between 'int?' and 'int' in C#?

I am 90% sure I saw this answer on stackoverflow before, in fact I had never seen the "int?" syntax before seeing it here, but no matter how I search I can't find the previous post, and it's driving me crazy. It's possible that I've been eating the…
cori
  • 8,666
  • 7
  • 45
  • 81
92
votes
6 answers

Find type of nullable properties via reflection

I examine the properties of an object via reflection and continue processing the data type of each property. Here is my (reduced) source: private void ExamineObject(object o) { Type type = default(Type); Type propertyType = default(Type); …
user705274
  • 923
  • 1
  • 6
  • 6
91
votes
3 answers

How to make a view column NOT NULL

I'm trying to create a view where I want a column to be only true or false. However, it seems that no matter what I do, SQL Server (2008) believes my bit column can somehow be null. I have a table called "Product" with the column "Status" which is…
René
  • 9,880
  • 4
  • 43
  • 49