-2

I want to handle "" and null while assigning value to the property of the class.

So how can i handle the same. Below is my example which works for null. But also want to handle empty string

 Id = characater.Id ?? System.Guid.NewGuid().ToString(),
user1037747
  • 1,307
  • 2
  • 18
  • 38

1 Answers1

2

Use string.IsNullOrEmpty along with the ?: Operator.

Id = string.IsNullOrEmpty(characater.Id) 
  ? System.Guid.NewGuid().ToString() 
  : characater.Id;

If you you also want to check for white space characters line spaces, line breaks, tabs, you can use String.IsNullOrWhiteSpace instead.

Igor
  • 60,821
  • 10
  • 100
  • 175