Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字符串解析为可为空的数字类型(1或2行)_C#_.net - Fatal编程技术网

C# 将字符串解析为可为空的数字类型(1或2行)

C# 将字符串解析为可为空的数字类型(1或2行),c#,.net,C#,.net,场景 将字符串解析为可为空的数字类型。如果解析不成功,则结果应为空;否则,结果应该是解析后的值 问题 为了实现这一点,我一直使用以下简单但又很烦人的方法: string numericString = "..."; decimal? numericValue; decimal temp; if (decimal.TryParse(numericString, out temp)) { numericValue = temp; } else { numericValue = nu

场景

将字符串解析为可为空的数字类型。如果解析不成功,则结果应为空;否则,结果应该是解析后的值

问题

为了实现这一点,我一直使用以下简单但又很烦人的方法:

string numericString = "...";

decimal? numericValue;
decimal temp;
if (decimal.TryParse(numericString, out temp))
{
    numericValue = temp;
}
else
{
    numericValue = null;
}
我使用上述方法是因为以下内容不可编译:

decimal temp;
decimal? numericValue = decimal.TryParse(numericString, out temp) ? temp : null;

有人知道第一位代码的版本和第二位代码一样短、整洁、可读吗?我知道我总是可以编写一个封装第一段代码的扩展方法,但我想知道,如果没有扩展方法,是否有任何方法可以实现我想要的功能。

一个简单的显式类型转换使其可编译:

decimal temp;
// typecast either 'temp' or 'null'
decimal? numericValue =
  decimal.TryParse(numericString, out temp) ? temp : (decimal?)null;
另一个选项是对所需的可空类型使用
default
运算符:

decimal temp;
// replace null with default
decimal? numericValue =
  decimal.TryParse(numericString, out temp) ? temp : default(decimal?);

我会这样做:

public delegate bool TryParseDelegate<T>(string str, out T value);

public static T? TryParseOrNull<T>(TryParseDelegate<T> parse, string str) where T : struct
{
    T value;
    return parse(str, out value) ? value : (T?)null;
}

decimal? numericValue = TryParseOrNull<decimal>(decimal.TryParse, numericString);
numericValue = numericString.TryParseDecimal();
public委托bool TryParseDelegate(字符串str,out T值);
公共静态T?TryParseOrNull(TryParseDelegate parse,string str),其中T:struct
{
T值;
返回解析(str,out value)?值:(T?)null;
}
十进制的numericValue=TryParseOrNull(decimal.TryParse,numericString);
或者您可以将其作为一种扩展方法:

public static T? TryParseAs<T>(this string str, TryParseDelegate<T> parse) where T : struct
{
    T value;
    return parse(str, out value) ? value : (T?)null;
}

decimal? numericValue = numericString.TryParseAs<decimal>(decimal.TryParse);
public static decimal? ParseDecimal(this string s)
{
    decimal d;
    if (decimal.TryParse(s, out d))
        return d;
    return null;
}
publicstatict?TryParseAs(这个字符串str,TryParseDelegate parse),其中T:struct
{
T值;
返回解析(str,out value)?值:(T?)null;
}
十进制的numericValue=numericString.TryParseAs(decimal.TryParse);

只需将其分解为一个扩展方法:

public static T? TryParseAs<T>(this string str, TryParseDelegate<T> parse) where T : struct
{
    T value;
    return parse(str, out value) ? value : (T?)null;
}

decimal? numericValue = numericString.TryParseAs<decimal>(decimal.TryParse);
public static decimal? ParseDecimal(this string s)
{
    decimal d;
    if (decimal.TryParse(s, out d))
        return d;
    return null;
}
然后您可以这样使用它:

public delegate bool TryParseDelegate<T>(string str, out T value);

public static T? TryParseOrNull<T>(TryParseDelegate<T> parse, string str) where T : struct
{
    T value;
    return parse(str, out value) ? value : (T?)null;
}

decimal? numericValue = TryParseOrNull<decimal>(decimal.TryParse, numericString);
numericValue = numericString.TryParseDecimal();
那么:

decimal? numericValue = 
    decimal.TryParse(numericString, out temp) ? temp : null as decimal?;

如果强制转换失败,这将使numericValue
null
,并且非常干净。

问题是temp是十进制的,null…是null。因为它们不是同一类型的,所以两者之间的比较失败,并且您会得到一个错误

尝试以下任一方法:

decimal? numericValue = decimal.TryParse(numericString, out temp) ? (decimal?)temp : null;


我认为您应该用一种方法来包装它,以提高可读性:

private decimal? ParseOrDefault(string decimalAsString, decimal? defaultIfInvalidString=null)
{
    decimal result;
    if (decimal.TryParse(decimalAsString, out result))
        return result;
    return defaultIfInvalidString;
}

[Test]
public void ParseOrDefaultTest()
{
    decimal? actual = ParseOrDefault("12", null);
    Assert.AreEqual(12m,actual);

    actual = ParseOrDefault("Invalid string", null);
    Assert.AreEqual(null, actual);
}

小数点?weight=decimal.TryParse(weightEditText.Text,out var tempWeight)?tempWeight:默认值(十进制?

?(十进制?)温度:空
只是为了添加另一个外观。@Ondrej如果您将
(十进制?)null
切换为
默认值(十进制?
,我将接受这个答案。@Dan,您可以在自己的代码中更改它,所以您最好现在就接受答案。@Thomaslevsque说得好。我并不是想达成协议。我唯一不喜欢的是它会抛出一个
InvalidCastException
。如果将
用作十进制?
,则如果强制转换失败,将返回空值。+1表示可用于
DateTime
bool
int
long
double
等的通用方法。