There is a certain if condition in which it says that if Vasya brought apples to Pete, then Petya will give Vasya Arbuz, the bottom line is that in the PHP world, if Vasya does not bring Pete's apples, then he will simply go on to go about his business, but in the C # world, Peter will not he will be able to eat and drink until Vasya brings him these very apples, and no matter how much, the main thing is not 0.

But seriously, then: RespFriends.error.error_code - which always exists, but the last 2 values ​​may or may not be when they are - everything is fine, but when they are not - an error occurs.

 if (@RespFriends.error.error_code == 14) { MessageBox.Show("Ура!"); } 

How can I turn it off or make the compiler go smarter?

    1 answer 1

    If RespFriends or RespFriends.error is null , then RespFriends.error.error_code results in a NullReferenceException error. It is necessary to add conditions for these cases:

     if(RespFriends != null && RespFriends.error != null && RespFriends.error.error_code == 14) { MessageBox.Show("Ура!"); } 

    And in C # 6.0 there is a new operator ?. , is called the null propagation operator (English) . If you use C # 6, you can do it like this:

     if (RespFriends?.error?.error_code == 14) { MessageBox.Show("Ура!"); } 
    • The dog can be removed, the author clearly tried to stifle the error. :) - Athari
    • @Discord So corrected. And I thought it was just part of the variable name. What does a doggie do in C #? I see it for the first time. - Peter Olson
    • A doggie in C # escapes keywords, turning them into ordinary identifiers. You can thus declare variables or fields with names if , while , etc. - Pavel Mayorov