C
C#16mo ago
TigerThePleb

Implement string properties

1, public string Prop { get; set} = string.Empty; 2, public string Prop { get; set} = ""; 3, public string Prop { get; set} = default!; Hi, I am a newbie, could some one help me to have better understand use case of those 3 way implement string properties above ? ( especially 3rd way) Thanks in advanced!
13 Replies
vdvman1
vdvman116mo ago
The first two are equivalent, they use an empty string as the default value The last makes it null instead of an empty string, and since you're using ! it looks like you are using nullable reference types, and since you're storing null into a property that states it isn't null you are potentially going to cause issues If you wanted to allow Prop to have a null value, use string? instead of string, and then you can remove the !
TigerThePleb
TigerThePleb16mo ago
Is 3rd way is wrong way or could have some design intended?
Henkypenky
Henkypenky16mo ago
the default value is null for reference types it's redundant this
public string Prop { get; set; }
public string Prop { get; set; }
and this
public string Prop { get; set} = default!;
public string Prop { get; set} = default!;
are both null they are the same
MODiX
MODiX16mo ago
Henkypenky#4865
REPL Result: Success
Test test = new();

Console.WriteLine(test.Prop1 is null);
Console.WriteLine(test.Prop2 is null);

class Test
{
public string Prop1 { get; set; }
public string Prop2 { get; set; } = default!;
}
Test test = new();

Console.WriteLine(test.Prop1 is null);
Console.WriteLine(test.Prop2 is null);

class Test
{
public string Prop1 { get; set; }
public string Prop2 { get; set; } = default!;
}
Console Output
True
True
True
True
Compile: 644.741ms | Execution: 96.305ms | React with ❌ to remove this embed.
Henkypenky
Henkypenky16mo ago
C# always had nulls but now you get more help to deal with them in your case if you don't use the null forgiving ! it will shout at you
Henkypenky
Henkypenky16mo ago
Henkypenky
Henkypenky16mo ago
this means in a NON NRT world
TigerThePleb
TigerThePleb16mo ago
so it's just for bypass waring right?
Henkypenky
Henkypenky16mo ago
when an instance is created it must contain a value, because the default constructor will initialize as null but in NRT world if you mark it as nullable it doesn't matter the compiler knows you said: hey this can be null, chill out yes, it's called the null forgiving operator I highly discourage you from using it
TigerThePleb
TigerThePleb16mo ago
yeah im across some internet code sample, just want get a better understand of it
vdvman1
vdvman116mo ago
Whether you use: - string and string.Empty - string? and null - string and the required keyword - string and initialise it in the constructor Depends on what values you want the property to hold and how you want it to be set the first time
TigerThePleb
TigerThePleb16mo ago
Marko Hrnčić
Code Maze
Realistic Data Generation in .NET With Bogus - Code Maze
Learn how to generate realistic data with the Bogus .NET library and save your time when mocking data and seeding databases with EF Core
Henkypenky
Henkypenky16mo ago
Bogus is really god good
Want results from more Discord servers?
Add your server
More Posts