c# - Is using default(string) a common idiom when comparing against the result of Linq xxxOrDefault? -


i'm reviewing code:

private static bool trygetvalidatedhost(string unvalidatedhost, out string validatedhost) {     validatedhost = _whitelistedhostnames.firstordefault(         hostname =>             hostname.equals(                 unvalidatedhost,                 stringcomparison.invariantcultureignorecase));      return (validatedhost != default(string)); } 

at first struck me odd compare validatedhost against default value string, i'd have expected comparison against null.

return (validatedhost != null); 

then thought, "more correct" - linq statement above returns firstordefault, should compared against default, happens default string null.

is there standard pattern this? should there clear preference, or style, , left author?

i'd relatively unusual compared null (or 0 or new somevaluetype() other types) not because default rare because null common. i'd therefore favour null reason.

i wouldn't consider default(string) unusual weird though. firstordefault of course returns default(tsource) because can used both nullable , non-nullable types it's reasonable coder calling think "if there's no match return default(string)" , compare that, rather "if there's no match return default(string) null because string nullable type" , compare null.

as such i'd consider reasonable stylistic choice. i'd still disagree stylistic choice , replace null if style guide project me , reviewing code, it's not far off disagreeing brace style, etc.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -