c#速记,如果不为null,则赋值

时间:2019-06-06 21:58:26

标签: c# shorthand null-coalescing-operator null-coalescing

现在C#中是否有任何速记可以削减以下代码:

var testVar1 = checkObject();
if (testVar1 != null)
{
      testVar2 = testVar1;
}

在这种情况下,如果CheckObject()结果中的testVar1不为null,则只想分配testVar2(testVar2具有将触发代码的setter)。试图思考如何使用null合并的东西,但不能真正解决问题。

添加到此testVar2的代码将在设置器上触发,因此,如果值为null,则不要将testVar2设置为任何值。

    public MyObj testVar2
    {
        get { return _testVar2; }
        set
        {
            _testVar2 = value;
            RunSomeCode();                
        }
    }

6 个答案:

答案 0 :(得分:4)

有一对!

三元运算符:

testvar2 = testVar1 != null ? testvar1 : testvar2;

逻辑将完全相同。

或者,如前所述,您可以使用空合并运算符:

testVar2 = testVar1 ?? testVar2

(尽管现在也有评论)

或第三个选项:编写一次方法,然后按自己的意愿使用它:

public static class CheckIt
{
    public static void SetWhenNull(string mightBeNull,ref string notNullable)
    {
        if (mightBeNull != null)
        {
            notNullable = mightBeNull;
        }
    }
}  

并命名为:

CheckIt.SetWhenNull(test1, ref test2);

答案 1 :(得分:4)

我用谷歌搜索“如果空则设置c#速记”,并首先登陆此处,仅对其他人有效。问题是“如果不是null则分配值的简写”,以下是“如果不是null则分配值的简写”。

在C#8.0+中,您可以使用??=

// Assign to testVar1, if testVar2 is null
testVar2 ??= testVar1;

// Which is the same as:
testVar2 = testVar2 ?? testVar1;

// Which is the same as:
if(testVar2 == null)
{
   testVar2 = testVar1;
}

我的最爱:

// Create new instance if null:
testVar1 ??= new testClass1();

// Which is the same as:
if(testVar1 == null)
{
   testVar1 = new testClass1();
}

这是我经常使用的示例:

List<string> testList = null;

// Add new test value (create new list, if it's null, to avoid null reference)
public void AddTestValue(string testValue)
{
   testList ??= new List<string>();
   testList.Add(testValue);
}

答案 2 :(得分:0)

您提到 char str[] = "Hello world\n"; for (int i = 0; str[i]; i++) str[i] = tolower((unsigned char)str[i]); 的设置器会在设置事件时触发某些事件。如果您不检查自己是否设置了testVar2,该事件仍将使用空合并运算符(或三元运算符)触发。

我认为您要么必须检查testVar2在其设置器中是否已设置为自身,要么必须执行您现在正在做的事情。

testVar2

我认为这完全是我的意见,但现在我将如何处理。我认为它传达的意图要比空手更好。

public MyObj testVar2 { get { return _testVar2; } set { if (_testVar2 != value) { _testVar2 = value; RunSomeCode(); } } } 说,“将testVar2 = testVar1 ?? tesrVar2设置为testVar2。除非testVar1为空。然后将testVar1设置为testVar2”。

testVar2

说:“如果if (testVar1 != null) { testVar2 = testVar1; } 不为空,请将testVar1设置为testVar2”。

答案 3 :(得分:0)

如果您永远不希望将testVar2设置为null,那么在设置器中检查null可能更有意义。否则,您必须记住随时尝试设置null

请注意,我还修改了外壳以符合C#标准

private MyObj testVar2;

public MyObj TestVar2
{
    get { return testVar2; }
    set
    {
        if (value == null) return;      // Or throw an ArgumentNullException
        if (testVar2 == value) return;  // No need to RunSomeCode if the value isn't changing

        testVar2 = value;
        RunSomeCode();                
    }
}

现在,如果您担心的仍然是在设置属性之前检查null(例如,如果您决定在null值的情况下抛出异常),那么就什么都没有了原始代码有误(三元/空集结解决方案似乎有点“浪费”,因为它们可能会给自己设置一些东西),您可以在一行中完成:

if (testVar1 != null) SomeClass.TestVar2 = testVar1;

但是,如果您只是创建testVar1来捕获对CheckObject()的调用结果,那么null营销运算符就更有意义了,因为您没有创建一个变量来存储值:

SomeClass.TestVar2 = CheckObject() ?? SomeClass.TestVar2;

答案 4 :(得分:0)

当前没有发布任何版本(最高7.3),但它将在C#8.0上运行(已经在预览中)。

您可以检查实施状态here和讨论情况here

答案 5 :(得分:-1)

您可以使用null-coalescing operator,它看起来像这样:testVar2 = testVar1 ?? testVar2。如果?? testVar2为空,则可以将testVar1替换为您要设置的内容。

相关问题