© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
16 replies
SWEETPONY

✅ How to automatically check for null?

I have following code:

public IActionResult Put(                                              
    Guid id,                                                           
    UpdateItemDto updateItemDto)                                       
{                                                                      
    var existingItem = Items                                           
        .FirstOrDefault(item => item.Id == id);                        
                                                                       
    var updatedItem = existingItem with                                
    {                                                                  
        Name = updateItemDto.Name,                                     
        Description = updateItemDto.Description,                       
        Price = updateItemDto.Price                                    
    };                                                                 
                                                                       
    var index = Items.FindIndex(existingItem => existingItem.Id == id);
    Items[index] = updatedItem;                                        
                                                                       
    return NoContent();                                                
}                                                                      
public IActionResult Put(                                              
    Guid id,                                                           
    UpdateItemDto updateItemDto)                                       
{                                                                      
    var existingItem = Items                                           
        .FirstOrDefault(item => item.Id == id);                        
                                                                       
    var updatedItem = existingItem with                                
    {                                                                  
        Name = updateItemDto.Name,                                     
        Description = updateItemDto.Description,                       
        Price = updateItemDto.Price                                    
    };                                                                 
                                                                       
    var index = Items.FindIndex(existingItem => existingItem.Id == id);
    Items[index] = updatedItem;                                        
                                                                       
    return NoContent();                                                
}                                                                      


existingItem
existingItem
can be null so I will have NRE in this line
var updatedItem = existingItem with
var updatedItem = existingItem with
.

yes, we can solve this issue by checking
existingItem
existingItem
for null but I don't want to do this every time, code looks much better without any checks so how to avoid this?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to check options for null?
C#CC# / help
3y ago
Null check
C#CC# / help
15mo ago
NullReferenceException but checking for Null?
C#CC# / help
2y ago
✅ Null check not enough to safely convert from nullable enum
C#CC# / help
3y ago