79784360

Date: 2025-10-07 08:56:10
Score: 1.5
Natty:
Report link

Title:

How to safely handle null values in c # inline (ternary operator)

Answer:

you can handle this safely using the null-conditional operator (?.) with string.IsNullOrEmpty:

var x=string.IsNullOrEmpty(ViewModel.OccupationRefer?.ToString())? string.Empty: ViewModel.OccupationRefer.ToString();

Explanation:

This way, x will be an empty string if OccupationRefer is null, otherwise it will contain the value.

Tip: using the ?. operator is safer than calling.ToString() directly because it prevents a NullReferenceException.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Kurmapu Hymavathi