I want to check an objects for null be for using it in assignment statement in C#.
Consider the following C# class definitions:
class Foo
{
public int FooId { get; set; }
public Bar FooBar { get; set; }
}
class Bar
{
public int BarId { get; set; }
}
Consider the following code snippet:
var foo1 = new Foo()
{
FooId = 1,
FooBar = new Bar()
{
BarId = 2
}
};
var foo2 = new Foo()
{
FooId = 1,
};
foo1.FooBar.BarId = foo2.FooBar == null ? foo2.FooBar.Barid : 0;
The following works fine, but seems a little outdated.
foo1.FooBar.BarId = foo2.FooBar == null ? foo2.FooBar.Barid : 0;
In C# 8 is there a different/better way to write the following using null coalescing?