C# Nullable String Error

C# nullable string error

System.String is a reference type and already "nullable".

Nullable<T> and the ? suffix are for value types such as Int32, Double, DateTime, etc.

How can I make my string property nullable?

String is a reference type and always nullable, you don't need to do anything special. Specifying that a type is nullable is necessary only for value types.

EF Core fail to load entity with nullable string value

This error means that the ProjectSaleTypeId is a non-nullable string property but your database table is allowing NULL values for that column.

If EF is handling your database it should not happen, but it might occur that you accidentally changed the DB column type and didn't migrate properly.

Enforce REAL non-nullable string reference type

You can add TreatWarningsAsErrors option to your csproj file

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

or add CS8625 warning to WarningsAsErrors list

<WarningsAsErrors>NU1605;CS8625</WarningsAsErrors>

And this code will generate an expected error

var test = new FooClass(testString: null, testDate: default);

error CS8625: Cannot convert null literal to non-nullable reference type.

Nullable reference types are implemented as type annotations in CLR, this can be a reason that compiler shows you an error with testDate in your original sample first.

var test = new FooClass(testString: null, testDate: null);

When you get rid of this error, you'll see the expected behavior with nullable references errors/warnings

Class in c# (with filehelpers) - nullable strings giving an error when other nullable types aren't

string is reference type, reference types are nullable in their nature.

When you define public string ItemNum, it is already nullable.

Nullable struct was added to allow make value types nullable too.

When you declare public decimal? ItemNum, it is equivalent to public Nullable<decimal> ItemNum.

Nullable struct has definition:

public struct Nullable<T> where T : struct, new()

where T : struct means that T can be only value type.

Description in MSDN is very detailed Nullable Structure.

Quote:

For example, a reference type such as
String is nullable, whereas a value
type such as Int32 is not. A value
type cannot be nullable because it has
enough capacity to express only the
values appropriate for that type; it
does not have the additional capacity
required to express a value of null.

Throw an error when the user enter null or empty string

Your if state is wrong. Let's do a truth table:

if (value != String.Empty || value != null)

Name = null   True Or False  = True 
Name = "name" True Or True = True
Name = "" False Or True = True

Your if statement is always true!

I would re-write it thus:

if (value == String.Empty || value == null)
{
throw new ArgumentException("Name cannot be null or empty string", "Name");
}
else
{
name = value;
}

you could just change the Or to and AND but I think the above reads better (the below has an unnecessary double negative):

if (value != String.Empty && value != null)
{
name = value;
}
else
{
throw new ArgumentException("Name cannot be null or empty string", "value");
}

As Dmitry Bychenko says, I didn't notice you were not testing for value. In getters you should use the value property. Not the name of your property


The second parameter (again pointed out by Dmitry Bychenko) in your exception should be:

The name of the parameter that caused the current exception.

MSDN

which in your case is the string "value":

throw new ArgumentException("Name cannot be null or empty string", "value");

Distinguish an explicitly nullable string type in C#

@Jesse got me pointed in the right direction. If anyone comes looking later, this is how I ended up checking and it seems to be working across the board:

if(modelProperty.PropertyType == typeof(string)
&& modelProperty.GetMethod?.CustomAttributes.Where(x => x.AttributeType.Name == "NullableContextAttribute").Count() == 0){
nullable = true;
}

Still glad to entertain any more succinct answers!



Related Topics



Leave a reply



Submit