✅ How to automatically check for null?
I have following code:
yes, we can solve this issue by checking
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();
} existingItemexistingItem can be null so I will have NRE in this line var updatedItem = existingItem withvar updatedItem = existingItem with.yes, we can solve this issue by checking
existingItemexistingItem for null but I don't want to do this every time, code looks much better without any checks so how to avoid this?