Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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# 有没有更好的方法来重新编写下面的代码?_C#_C# 4.0 - Fatal编程技术网

C# 有没有更好的方法来重新编写下面的代码?

C# 有没有更好的方法来重新编写下面的代码?,c#,c#-4.0,C#,C# 4.0,有没有更好的方法写下面的内容 select new { PaymentId = String.IsNullOrEmpty(p.PaymentId) ? "" : p.PaymentId, AgencyId = String.IsNullOrEmpty(p.AgencyId) ? "" : p.AgencyId, ............... ................ 您可以创建一个扩展方法: public static class StringExtens

有没有更好的方法写下面的内容

    select new
    {
   PaymentId = String.IsNullOrEmpty(p.PaymentId) ? "" : p.PaymentId,
   AgencyId = String.IsNullOrEmpty(p.AgencyId) ? "" : p.AgencyId,
...............
................

您可以创建一个扩展方法:

public static class StringExtensions
{
    public static GetValueOrStringEmpty(this string input)
    {
        return string.IsNullOrEmpty(input) 
              ? string.Empty 
              : input;
    }
}
然后,您可以按如下方式重构代码:

select new
{
    PaymentId = p.PaymentId.GetValueOrStringEmpty()
    AgencyId =  p.AgencyId.GetValueOrStringEmpty(),
    // ...
}

免责声明方法的名称
GetValueOrStringEmpty
可能不是最合适的。因此,我建议您考虑一个更直观的名称。

您可以创建一个扩展方法:

public static class StringExtensions
{
    public static GetValueOrStringEmpty(this string input)
    {
        return string.IsNullOrEmpty(input) 
              ? string.Empty 
              : input;
    }
}
然后,您可以按如下方式重构代码:

select new
{
    PaymentId = p.PaymentId.GetValueOrStringEmpty()
    AgencyId =  p.AgencyId.GetValueOrStringEmpty(),
    // ...
}

免责声明方法的名称
GetValueOrStringEmpty
可能不是最合适的。因此,我建议您考虑一个更直观的名称。

如果您确定p不是
null
,您可以编写

PaymentId = p.PaymentId ?? "";

如果PaymentId为null,那么如果您确定p不是null,那么它将返回一个空字符串

PaymentId = p.PaymentId ?? "";

如果PaymentId为null,它将返回一个空字符串

您可以在C#中使用null合并运算符


可以在C#中使用空合并运算符


你也可以这样使用它-

   select new
    {
      PaymentId = p?.PaymentId ?? string.Empty,
      AgencyId = p?.AgencyId ?? string.Empty,
参考资料:


您也可以这样使用它-

   select new
    {
      PaymentId = p?.PaymentId ?? string.Empty,
      AgencyId = p?.AgencyId ?? string.Empty,
参考资料:


string.Concat(p.PaymentId)
p.PaymentId??string.Empty
string.Concat(p.PaymentId)
p.PaymentId??string.Empty
如果p可以为null,则使用:PaymentId=p?.PaymentId??"". 如果p为null,则也将返回“.@RuardvanElburg这在C#6.0中是正确的,但是,他确实将问题标记为C#4.0问题,如果p可以为null,则使用:PaymentId=p?.PaymentId??"". 如果p为null,这也将返回“”。@RuardvanElburg在C#6.0的情况下也是如此,但是,他确实将该问题标记为C#4.0问题