小编use*_*758的帖子

在处理不可为空类型的空值的 setter 上停止 C# 警告“可能的空引用分配”

public class Example
{
    public string Name
    {
        get => m_name;
        set => m_name = value ?? string.Empty; // valid to assign null cause it's handled but caused warnings
    }
    private string m_name;

    public Example(string? name)
    {
        Name = name; // CS8601 - Possible null reference assignment.
    }
}

// imagine this is setting on an instance and not the type
Example.Name = data; // CS8601 - Possible null reference assignment.
Run Code Online (Sandbox Code Playgroud)

在代码中,我有一个string永远不为空的类型,因为属性设置器将空赋值转换为默认值。编译器不理解这一点并且对此抱怨不已。

我知道我可以通过以下方式修复其中一些问题:

  • 放在!赋值之后,因为我比编译器更了解 - 不喜欢这样,因为它可能会因重构而中断 …

c# null compiler-warnings

3
推荐指数
1
解决办法
671
查看次数

如何在控制台项目中重写一行文本?C++

我正在开发一个c ++控制台项目,我想显示一个百分比,而不是每次都创建一个新行(这样窗口就不会被数千行阻塞).

有没有办法删除打印的最后一行或者说下次我输出一行它应该替换当前行?

c++

2
推荐指数
2
解决办法
5405
查看次数

标签 统计

c# ×1

c++ ×1

compiler-warnings ×1

null ×1