C# 中的三元运算符看起来代码更干净
Ternary operator in C# to look code cleaner
很抱歉问了这个愚蠢的问题。
我知道三元运算符让开发人员的生活变得非常轻松。我有个疑问。
假设我必须检查某些东西是否为 DBNull.Value
,如果是则分配 '0' 或保持原值。我这样做如下
string str = dt.Rows["Col"] == DBNull.Value ? "0" : dt.Rows["Col"].ToString():
它工作正常,但如果我的表达式很大,那么它会产生像
这样丑陋的代码
int CL = (int)Math.Round((Convert.ToInt32(dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'") == DBNull.Value ? 0 : dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'")) * Total_Leaves) / 100d);
看看其中重复的代码。能不能再简单点。
我知道 ??
是检查 null
的一种更简洁的方法 DBNull.Value
或 "" ?
谢谢
Can I make it look little simpler.
是的,此时您的方法会检查 compute 是否为空,然后 returns 0 如果是,那么您可以从提取通用方法开始
var computed = dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'");
int CL = (int)Math.Round((Convert.ToInt32(computed == DBNull.Value ? 0 : computed) * Total_Leaves) / 100d)
但是您可以更进一步,将其放入一个方法中,然后如果它为 null 则返回 0
private int GetComputed(int Total_Leaves)
{
var computed = dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'");
if(computed == DBNull.Value)
return 0;
return (int)Math.Round(Convert.ToInt32(computed) * Total_Leaves) / 100d;
}
您可以用类似下面的简单方法包装测试:
public static string GetString(Object testObject)
{
return testObject == DBNull.Value ? "0" : testObject.ToString();
}
它基本上是一样的,但你没有到处进行三元测试。
你的代码应该是这样的:
(int)Math.Round((Convert.ToInt32(GetString(dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'"))) * Total_Leaves) / 100d);
尽管它要求您转换字符串结果 - 所以不是适用于所有情况的完美解决方案。
我为此使用了扩展方法。感谢 mike z
的评论:)
public static class Extensions
{
public static object DefaultIfDBNull(this object obj, object _default)
{
return obj == DBNull.Value ? _default : obj;
}
}
和调用代码
int CL = (int)Math.Round((Convert.ToInt32(dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'").DefaultIfDBNull(0)) * Total_Leaves) / 100d);
虽然这不是一个正确的答案,但它使它更干净和可用:)
很抱歉问了这个愚蠢的问题。
我知道三元运算符让开发人员的生活变得非常轻松。我有个疑问。
假设我必须检查某些东西是否为 DBNull.Value
,如果是则分配 '0' 或保持原值。我这样做如下
string str = dt.Rows["Col"] == DBNull.Value ? "0" : dt.Rows["Col"].ToString():
它工作正常,但如果我的表达式很大,那么它会产生像
这样丑陋的代码int CL = (int)Math.Round((Convert.ToInt32(dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'") == DBNull.Value ? 0 : dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'")) * Total_Leaves) / 100d);
看看其中重复的代码。能不能再简单点。
我知道 ??
是检查 null
的一种更简洁的方法 DBNull.Value
或 "" ?
谢谢
Can I make it look little simpler.
是的,此时您的方法会检查 compute 是否为空,然后 returns 0 如果是,那么您可以从提取通用方法开始
var computed = dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'");
int CL = (int)Math.Round((Convert.ToInt32(computed == DBNull.Value ? 0 : computed) * Total_Leaves) / 100d)
但是您可以更进一步,将其放入一个方法中,然后如果它为 null 则返回 0
private int GetComputed(int Total_Leaves)
{
var computed = dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'");
if(computed == DBNull.Value)
return 0;
return (int)Math.Round(Convert.ToInt32(computed) * Total_Leaves) / 100d;
}
您可以用类似下面的简单方法包装测试:
public static string GetString(Object testObject)
{
return testObject == DBNull.Value ? "0" : testObject.ToString();
}
它基本上是一样的,但你没有到处进行三元测试。
你的代码应该是这样的:
(int)Math.Round((Convert.ToInt32(GetString(dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'"))) * Total_Leaves) / 100d);
尽管它要求您转换字符串结果 - 所以不是适用于所有情况的完美解决方案。
我为此使用了扩展方法。感谢 mike z
的评论:)
public static class Extensions
{
public static object DefaultIfDBNull(this object obj, object _default)
{
return obj == DBNull.Value ? _default : obj;
}
}
和调用代码
int CL = (int)Math.Round((Convert.ToInt32(dtByType.Compute("sum(NumberOfLeaves)", "Type = 'Casual Leave (Percent)'").DefaultIfDBNull(0)) * Total_Leaves) / 100d);
虽然这不是一个正确的答案,但它使它更干净和可用:)