FROM#?? combined with ?: question

I create an XML Deserializer for a project, and I often come across this type of code:

var myVariable = ParseNDecimal(xml.Element("myElement")) == null ? 
                 0 : ParseNDecimal(xml.Element("myElement")).Value;

Is there a better way to write this statement?

EDIT: Perhaps I should have clarified my example, since I have a helper method for parsing strings in a decimal system.

+3
source share
3 answers

You can use the extension method:

public static T TryGetValue<T>( this XmlElement element ) {
    if ( null == element ) return default(T);
    return (T)element.Value;
}
...
...
var myVariable = xml.Element("myElement").TryGetValue<decimal>();

EDIT:

"Universal" solution:

static class Program {
    static void Main() {
        var xmlDecimal = new XElement( "decimal" );
        xmlDecimal.Value = ( 123.456m ).ToString();
        decimal valueOfDecimal_1 = xmlDecimal.ValueAs<decimal>( decimal.TryParse );
        bool valueOfBool_1 = xmlDecimal.ValueAs<bool>( bool.TryParse );

        var xmlBool = new XElement( "bool" );
        xmlBool.Value = true.ToString();
        decimal valueOfDecimal_2 = xmlBool.ValueAs<decimal>( decimal.TryParse );
        bool valueOfBool_2 = xmlBool.ValueAs<bool>( bool.TryParse );
    }
}

public static class StaticClass {
    public delegate bool TryParseDelegate<T>( string text, out T value );
    public static T ValueAs<T>( this XElement element, TryParseDelegate<T> parseDelegate ) {
        return ValueAs<T>( element, parseDelegate, default( T ) );
    }
    public static T ValueAs<T>( this XElement element, TryParseDelegate<T> parseDelegate, T defaultValue ) {
        if ( null == element ) { return defaultValue; }

        T result;
        bool ok = parseDelegate( element.Value, out result );
        if ( ok ) { return result; }

        return defaultValue;
    }
}
+6
source

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 " ". , , . , .

+5

, , , ...

Element() null, , , ? . (decimal):

var myVariable 
    = (decimal)(xml.Element("myElement") ?? new XElement("myElement", 0));

, , , . YMMV.

+1

Source: https://habr.com/ru/post/1702893/


All Articles