c# - Which is better return null or empty object? -


this question has answer here:

which better return null or empty object?

var entity = getfromdb(); if (entity != null) {     var price = entity.price != null          ? pricedecorator.decorate(entity.price)          : null; }      public class pricedecorator  {      public static string decorate(decimal? price)      {          if (price == null || price == 0)              return string.empty;           return string.format(cultureinfo.currentculture , "{0:c}", price);      }  } 

in above code, return null , return sting.empty, don't know return indicate 'no data'.

any advice?

thanks in advance

it depends:

  • when return string.empty on property of object, means entity exist, object doesn't have property or property in fact string no characters.

  • when return null instead of string.empty means the entry/entity doesn't exists, in other words, there no data


Comments