If I write
public MyObject TheObject {get; set;}
and later try to access myClassObject.TheObject.SomeProperty;
if TheObject
is null I get a NullReferenceException. But how I can declare the TheObject
properties to avoid an NRE if TheObject
can be null or have a default value.
Not sure if I explain correctly what is my concern. Here is what I'm trying to do safely. What is the best practice to do that.
class PowerfullPin
{
public string Id { get; set; }
}
class Test
{
public PowerfullPin PowerfullPin { get; set; }
}
class Main
{
Main()
{
Test Test = new Test();
//If PowerfullPin was not define it throws a NullReferenceException
System.Diagnostics.Debug.WriteLine(Test.PowerfullPin.Id);
}
}