c# - VS2017只用getter封装?

标签 c# visual-studio refactoring visual-studio-2017

在 Visual Studio 中,您可以使用重构来自动封装字段:

protected bool grounded

变成了

private bool grounded;

protected bool Grounded
{
    get
    {
        return grounded;
    }
    set
    {
        grounded = value;
    }
}

这非常方便,但大多数时候我只想生成 getter 而不是 getter 和 setter,有没有办法做到这一点?

我似乎在任何地方都找不到它,所以我每次都必须删除 setter 。

最佳答案

如果您的实现是默认实现,您可以使用自动属性并编写:

public bool Grounded { get; set; }

然后使用 setter :

public bool Grounded { get; }
// or
public bool Grounded { get; private set; }

它基本上会编译为您编写的内容。 在 VS 中,你有一个宏:编写 prop 并按 Tab+Tab ,它将生成上面的行

关于c# - VS2017只用getter封装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43595466/

相关文章:

c# - 默认排除条件和扩展以将它们包括回来

c# - Hololens - UserConsentVerifier 不适用于 Hololens 第一代

c++ - 将字符串存储到二维数组中

visual-studio - MSBuild + Visual Studio : Customize default "Copy Local" for all projects in the solution

c - Visual Studio : Callback function problem (__cdecl *)

c# - .net 5 Azure b2c 注销循环

c# - IEnumerable 属性中的 LINQ 过滤会破坏绑定(bind)

refactoring - Python list-comprehensions 中的 lisp-style style `let` 语法

python - 从 Google App Engine 模型迁移到纯 Django 系统

c# - 重构我的代码 : Conditions based on different variables