1

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); 
    }
}
mason
  • 31,774
  • 10
  • 77
  • 121
SunLiker67
  • 119
  • 6
  • Safe? What are you talking about? Is your computer going to explode? But seriously, I don't get it. – rory.ap May 17 '17 at 12:53
  • @rory.ap I believe the OP means how to protect against null references on properties. – Matt Hogan-Jones May 17 '17 at 12:54
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – quetzalcoatl May 17 '17 at 12:54
  • 3
    This is an interesting example of the Law of Demeter. – Matt Hogan-Jones May 17 '17 at 13:50
  • I did not know this law, thank you for quoting it, it is very instructive on the method to use in this case. If I understand correctly, A will call the methods of B such as B.PowerfullPinId which will return PowerfullPin? .Id. Is it correct? – SunLiker67 May 17 '17 at 14:09
  • 1
    @SunLiker67 yes, that's about it. This is an article I found to be very useful : http://haacked.com/archive/2009/07/14/law-of-demeter-dot-counting.aspx/ – Matt Hogan-Jones May 17 '17 at 14:26

2 Answers2

4

Try with null conditional operator if using c# 6 or above. It's used to test for null before performing a member access (?.) or index (?[) operation.

https://learn.microsoft.com/en-us/dotnet/articles/csharp/language-reference/operators/null-conditional-operators

class PowerfullPin
{
    public string Id { get; set; }
}

class Test
{
    public PowerfullPin PowerfullPin { get; set; }
}

class Main
{
    Main()
    {
        Test Test = new Test();
        System.Diagnostics.Debug.WriteLine(Test.PowerfullPin?.Id);
    }
}
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • it depends of wich .net version the OP is using – BRAHIM Kamel May 17 '17 at 12:58
  • @BRAHIMKamel I mentioned that in my answer and OP haven't specified any version. – Oscar May 17 '17 at 12:59
  • 1
    You should explain what the null conditional operator does in your question, rather than relying on a link for the explanation. – mason May 17 '17 at 13:00
  • 1
    @BRAHIMKamel No, it doesn't. It depends on the version of C#, not the framework. – Servy May 17 '17 at 13:14
  • @Servy Ok that's right if you use a nuget package manager I know that this it's jus a syntatic sugar. But to make it simple I mentioned the .net version given you can't do C# 6.0 without installing the .net 4.6 or upper Right? – BRAHIM Kamel May 17 '17 at 13:18
  • @BRAHIMKamel No, that's false. You can write a C# 6.0 program targeting .NET 1.0. – Servy May 17 '17 at 13:18
  • @Servy I know this It's just a syntatic sugar as I told you but you will be constrained to do the compiler work right ? – BRAHIM Kamel May 17 '17 at 13:20
  • 1
    @BRAHIMKamel You'll need to have C# 6.0, as I said. You can target *any .NET framework that you want* with the program though, so no, there is no constraint on the .NET framework that you need to use, contrary to your comment. – Servy May 17 '17 at 13:25
  • @Servy So if I understand you I can target c# 6.0 on visual studio 2013 and .net 4.5 installed if this is the case show me how – BRAHIM Kamel May 17 '17 at 13:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144471/discussion-between-brahim-kamel-and-servy). – BRAHIM Kamel May 17 '17 at 13:59
  • @BRAHIMKamel If you want to know how to change the .NET version of a C# project then you can do a Google search and you'll be able to find instructions on how to do that. – Servy May 17 '17 at 14:40
  • @Servy I had just googled an what I found it's just a workaround on how to install roslyn not more I tought that you have your own idea – BRAHIM Kamel May 17 '17 at 14:51
  • @BRAHIMKamel Since apparently that was asking too much of you, [here](https://www.google.com/search?q=how+to+change+the+.NET+version+of+a+C%23+project&oq=how+to+change+the+.NET+version+of+a+C%23+projec) is a simple google search that searches for exactly what I told you to search for. – Servy May 17 '17 at 14:54
  • @Servy just be cool not at this point I can write a simple google query Thanks for all – BRAHIM Kamel May 17 '17 at 14:56
  • @Servy Just as I told you https://social.msdn.microsoft.com/Forums/en-US/49ba9a67-d26a-4b21-80ef-caeb081b878e/will-c-60-ever-be-supported-by-vs-2013?forum=roslyn the confirmation is here – BRAHIM Kamel May 17 '17 at 14:58
  • @BRAHIMKamel That link has nothing to do with this. The feature is a C# 6 feature, and a C# 6 program can target any framework, thus you don't need to have a particular framework version to use the feature, you can use any, so long as you have the right C# version. – Servy May 17 '17 at 15:02
  • @ Ok said like this I agree with you since there is no significant changes to the CLR since 2.0 Yes if the program is compiled it's can run with any framework but the intellisense the compiler are supported with .net 4.6 developer pack – BRAHIM Kamel May 17 '17 at 15:05
1

You are accessing property of a null object. PowerfullPin in Test class is null when you create object of Test.

So instead you can do like

Test test = new Test();
//While creating object of Test class you will have PowerfullPin 
//property but it has not been initialized. That means it is null. 
//You can not have properties of null object.
test.PowerfullPin = new PowerfullPin{
 Id = 1,
 SomeOtherProperty = "Value"
};
 System.Diagnostics.Debug.WriteLine(test.PowerfullPin.Id);
Bhuban Shrestha
  • 1,304
  • 11
  • 27