C# regarding Null Conditional Operator & Null Propagation

T.Zacks 3,996 Reputation points
2022-07-24T17:36:01.08+00:00

See the sample code first

class NullCheckOperator  
{  
    public static void Main()  
    {  
        person p = new person();  
        var name = p?.Name ?? "value cannot be null";  
        System.Console.WriteLine($"{name}");  
    }  
}  
class person  
{  
    public string Name { get; set; }  
}  

what is the meaning of this line p?.Name ?? "value cannot be null"; i know the meaning of null coalescing. if p,Name is null then show that message "value cannot be null"

but p? meaning not clear. please help me to understand it. thanks

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. ShuaiHua Du 636 Reputation points
    2022-07-25T06:09:55.337+00:00

    Let me translate the p?.Name ?? "value cannot be null" for you:

    p?.Name translated as below:

    if (p == null)  
    {  
        return null;  
    }  
    else  
    {  
        return p.Name;  
    }  
    

    if the return result is null, the p?.Name ?? "value cannot be null" expression with return "value cannot be null".

    So, sum up, the below code maybe help you for understanding:

    if (p == null || (p != null && p.Name == null))  
    {  
        return "value cannot be null";  
    }  
    else  
    {  
        return p.Name;  
    }  
    

    If right, please Accept.
    Enjoy Programming!!!

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2022-07-24T17:41:02.477+00:00

    ?. is the null-conditional operator, so in this case if p is null then the overall result of p?.Name is null:
    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-

    ?. effectively short-circuits the property accessor expression to do an "else" condition in the event that p is null.

    Once p?.Name has resolved to null the null-coalescing operator (??) will result in the fallback value of "value cannot be null" ending up as the value of name:
    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator

    3 people found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,591 Reputation points Volunteer Moderator
    2022-07-24T18:43:41.443+00:00

    A side note, when I see var name = p?.Name ?? "value cannot be null"; my first thought is why are you not using data annotations or fluent validation?

    public class Person  
    {  
        [Required]  
        public string Name { get; set; }  
    }  
    

    Some simple validating.

    And for not going with typical validations there is another way to check for null

    Person person = new();  
      
    Console.WriteLine("person.FirstName is { } firstName");  
      
    if (person.FirstName is { } firstName)  
    {  
    	Console.WriteLine($"\tFirst Name: {firstName}");  
    }  
    else  
    {  
    	Console.WriteLine("\tFirst name is null");  
    }  
    
     
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.