简体   繁体   English

为什么自动实现的属性必须同时定义 get 和 set 访问器

[英]Why Automatically implemented properties must define both get and set accessors

When we define a property like当我们定义一个属性时

    public string Name {get; set;}

dot.net can make our properties code. dot.net 可以制作我们的属性代码。 but when we use但是当我们使用

    public string Name {get;}
    public string Name {set;}

we face with我们面对

'Hajloo.SomeThing.PropertyName.set' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.

Actually why the compiler can't determine the property and make code automatically?其实为什么编译器不能判断属性并自动生成代码呢? What's the problem?有什么问题?

Because the auto-implemented properties generate their own backing store for the property values. 因为自动实现的属性为属性值生成自己的后备存储。 You have no access to the internal store. 您无权访问内部商店。

Implementing a property with 用。实现属性

  • just get : means you can only retrieve the values. just get:意味着你只能检索这些值。 You can't ever set the property value (even in the containing class) 您不能设置属性值(即使在包含类中)
  • just set : means you can only set the values. 只是设置:表示您只能设置值。 You can't retrieve the property value. 您无法检索属性值。

for a normal property 对于正常的财产

private int _data;
public int Data{  get { return _data } };

Here the parent class can do the following somewhere else in the class ( which it can't with auto props) 这里的父类可以在类中的其他地方执行以下操作(它不能使用自动道具)

_data = 100;

Note: You can define an auto-prop like this (which is how I use it the most). 注意:您可以像这样定义一个auto-prop(这是我最常用的方式)。

public int Data { get; private set;}

This means that the property can't be set by external clients of the class. 这意味着该类的外部客户端无法设置该属性。 However the containing class itself can set the property multiple times via this.Data = x; 但是包含类本身可以通过this.Data = x;多次设置属性this.Data = x; within the class definition. 在类定义中。

如果没有setter,则该属性永远不会具有除默认值之外的任何内容,因此不会用于任何目的。

A more modern scenario for receiving this error is building code that uses C#6 syntax using a version of VisualStudio that is less than VS 2015 (or using MsBuild that is less than 14). 接收此错误的更现代的方案是使用小于VS 2015(或使用小于14的MsBuild)的VisualStudio版本构建使用C#6语法的代码。

In C#6.0 it is allowable to have autoProperties that do not have a setter (they are assumed to be a private set). 在C#6.0中,允许使用没有setter的autoProperties(它们被假定为私有集)。

Try compiling with VS2015+ or msbuild 14+ .. or modify the code so that all autoProperties have a setter. 尝试使用VS2015 +或msbuild 14+编译..或修改代码,以便所有autoProperties都有一个setter。

How to assign default value in case of null in C#如何在 C# 中为 null 分配默认值

" Assign default value to object if value is null in c# [duplicate] " This top was closed saying that it got ansewred by this one. 如果 c# 中的值为 null,则将默认值分配给 object [重复] ”此顶部已关闭,表示已被此顶部回答。 However everything posted here does not answer that topic in my opinion, so I'm posting in here (not able to post in there) what worked for me and I was looking for:但是,我认为此处发布的所有内容都没有回答该主题,因此我在这里发布(无法在那里发布)对我有用并且我一直在寻找的内容:

decimal default_value = 0.00m;
return (decimal?)json['JSON_KEY'] ?? default_value;

In the example above I was trying to set a default value in case extraction of a decimal from a json is null (like we can do in kotlin or swift).在上面的例子中,我试图设置一个默认值,以防从 json 中提取小数是 null(就像我们可以在 kotlin 或 swift 中做的那样)。 Ps: This implementation works for new versions of .NET framework. Ps:此实现适用于新版本的 .NET 框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM