Edit: Given the edited question, this is much simpler.
Again he uses the extension method, but now there is no need to do the conversion in the method.
var myVariable = ParseNDecimal(xml.Element("myElement").ValueOrDefault("0"));
...
public static string ValueOrDefault(this XElement element,
string defaultValue)
{
return element != null ? element.Value : defaultValue;
}
, , object
ToString
, :
var myVariable = ParseNDecimal(xml.Element("myElement").ValueOrDefault(0m));
, . , ToString
.
, . ( , - - XAttribute
?) :
var myVariable = xml.Element("myElement").ValueOrDefault(0m);
...
public static decimal ValueOrDefault(this XElement element,
decimal defaultValue)
{
return element != null ?(decimal) element.Value : defaultValue;
}
, . , XAttribute
, generics - , , XAttribute
" ". , , . , .