Search code examples
c#blazorrazor-pages

.razor file vs .cs file IsNullOrEmpty() usage differences


In a .cs file, you can call IsNullOrEmpty() directly on a string without any issue (StringVariable.IsNullOrEmpty()). However, in a .razor file, you have to write String.IsNullOrEmpty(StringVariable) in order to call the function. What is the cause of this difference? Why isn't it standardized, considering both are in c# and the babies of Microsoft?


Solution

  • Variable.IsNullOrEmpty() is not built into the language. Often many developers will implement it as an extension method (example) you may have tripped and fallen into one of situations where its included in a dependency and you have access to it.

     public static class StringExtensions {
       public bool IsNullOrEmpty(this string mystring) => String.IsNullOrEmpty(mystring);
     }