tagged [nullable]

How do I specify "any non-nullable type" as a generic type parameter constraint?

How do I specify "any non-nullable type" as a generic type parameter constraint? The post is specific to C# 8. Let's assume I want to have this method: If my `.csproj` looks like this (i.e. C# 8 and n...

Cast object to decimal? (nullable decimal)

Cast object to decimal? (nullable decimal) If have this in the setter of a property: value = "90" But after the cast, temp is ... What is the proper way to do this cast?

8 Jan at 20:57

Correct way to check if a type is Nullable

Correct way to check if a type is Nullable In order to check if a `Type` ( `propertyType` ) is nullable, I'm using: ``` bool isNullable = "Nullable`1".Equals(propertyType.Name) ``` Is there some way t...

20 Jan at 10:28

How will a C# switch statement's default label handle a nullable enum?

How will a C# switch statement's default label handle a nullable enum? How will a C# switch statement's default label handle a nullable enum? Will the default label catch nulls and any unhandled cases...

19 Feb at 05:33

How can I convert decimal? to decimal

How can I convert decimal? to decimal may be it is a simple question but I'm try all of conversion method! and it still has error! would you help me? decimal? (nullable decimal) to decimal

Detecting a Nullable Type via reflection

Detecting a Nullable Type via reflection Surprisingly the following code fails the Assert: So just out curiosity, how can you determine if a given instance is a Nullable object or not?

17 May at 05:58

Why don't I get a warning about possible dereference of a null in C# 8 with a class member of a struct?

Why don't I get a warning about possible dereference of a null in C# 8 with a class member of a struct? In a C# 8 project with [nullable reference types](https://learn.microsoft.com/en-us/dotnet/cshar...

c# why can't a nullable int be assigned null as a value

c# why can't a nullable int be assigned null as a value Explain why a nullable int can't be assigned the value of null e.g What's wrong with that code?

15 Apr at 14:50

What is the memory footprint of a Nullable<T>

What is the memory footprint of a Nullable An `int` (`Int32`) has a memory footprint of 4 bytes. But what is the memory footprint of: and : Is this in general or type dependent?

4 Sep at 20:15

C# struct, how to assign a null value?

C# struct, how to assign a null value? I have a list: For some reason, it is not allowing me to assign null values to it. What do I do if I want to have no struct associated?

10 Nov at 20:36

Why int can't be null? How does nullable int (int?) work in C#?

Why int can't be null? How does nullable int (int?) work in C#? I am new to C# and just learned that objects can be null in C# but `int` can't. Also how does nullable int (`int?`) work in C#?

10 Jul at 07:9

Cannot implicitly convert type bool?

Cannot implicitly convert type bool? I am trying to convert my nullable bool value and I am getting this error.

7 May at 20:54

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and <null>

Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and Why does this not compile? > Type of conditional expression cannot be determined because t...

4 Aug at 16:25

Checking if Type instance is a nullable enum in C#

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

27 Apr at 16:33

Nullable DateTime?

Nullable DateTime? how to create setter and getter Properties for nullable datetime. for example: Does nullable attributes support setter and getter or have i to declare it public?

12 Jan at 17:57

Best practice for using Nullable Reference Types for DTOs

Best practice for using Nullable Reference Types for DTOs I have a DTO which is populated by reading from a DynamoDB table. Say it looks like this currently: ``` public class Item { public string Id...

Returning nullable string types

Returning nullable string types So I have something like this which doesn't compile. How do I return a nullable string type?

29 May at 22:41

Variable type ending with ?

Variable type ending with ? What does `?` mean: When applied to `string?`, there is an error: > The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic t...

29 Jun at 09:44

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

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 ...

10 May at 09:48

Which is preferred: new Nullable<int> or (int?)null?

Which is preferred: new Nullable or (int?)null? Which way is preferred in expressions like this: is it better to cast on `null` or create a `new Nullable` ?

29 Apr at 17:43

WPF Textbox accept INT but not NULLABLE INT?

WPF Textbox accept INT but not NULLABLE INT? Model XAML Once user try to or the value in this `textbox`, a message `Value '' cannot be converted`. May I know what's wrong with it?

15 Feb at 04:0

Is constructor the only way to initialize non-nullable properties in a class in C#?

Is constructor the only way to initialize non-nullable properties in a class in C#? I have switched to enable nullable in my project that uses C#8. Now I have the following class: Compiler of course c...

What is the use of Nullable<bool> type?

What is the use of Nullable type? a variable could hold true or false, while could be null as well. Why would we need a third value for bool ? If it is not , what ever it is, it is == Can you suggest ...

6 Feb at 16:7

How to convert C# nullable int to int

How to convert C# nullable int to int How do I convert a nullable `int` to an `int`? Suppose I have 2 type of int as below: I want to assign `v1`'s value to `v2`. `v2 = v1;` will cause an error. How d...

15 Mar at 06:40

Why nullable int (int?) doesn't increase the value via "+=" if the value is NULL?

Why nullable int (int?) doesn't increase the value via "+=" if the value is NULL? I have a page counter type of int?: It works ONLY if the value of ViewCount property is (any int). Why the compiler do...