代码之家  ›  专栏  ›  技术社区  ›  Jonathan Wood

参数为真时,指示结果不为空的属性?

  •  2
  • Jonathan Wood  · 技术社区  · 4 年前

    我有如下方法。

    public Node? GetLastNode(bool createNewIfEmpty = false)
    {
        // Return last node if any
        if (Nodes.Count > 0)
            return Nodes[Nodes.Count - 1];
    
        // Return a new appended node, if requested
        if (createNewIfEmpty)
        {
            Nodes.Add(new Node());
            return Nodes[0];
        }
    
        // Otherwise, return null
        return null;
    }
    

    createNewIfEmpty true ?

    0 回复  |  直到 4 年前
        1
  •  0
  •   Xerillio    4 年前

    我不确定 NotNullIfNotNull

    public Node? GetLastNode()
    {
        // Return last node if any. Otherwise, return null
        return Nodes.Count > 0
            ? Nodes[^1]
            : null;
    }
    
    public Node GetOrCreateLastNode()
    {
        // Add new node if the list is empty
        if (Nodes.Count == 0)
            Nodes.Add(new Node());
    
        // Return the last node
        return Nodes[^1];
    }
    
    推荐文章