简体   繁体   English

自动执行的属性错误

[英]Auto-Implemented Properties Error

I try to use AIP 我尝试使用AIP

public int AIP_NoSet
{
    get { ;}
}

Compiler say that it is an error: 编译器说这是一个错误:
Program.c1.AIP_NoSet.get': not all code paths return a value Program.c1.AIP_NoSet.get':并非所有代码路径都返回一个值

But even if I write 但是即使我写

public int AIP_NoSet { get { ;} set { ;} }

it shows me the same error. 它告诉我同样的错误。

Where am I wrong? 我哪里错了?

You should write 你应该写

public int AIP { get; set; }

when you write { ;} after it, this is seen as a syntacticaly incorrect attempt of a user implemented property. 当您在其后写{ ;}时,这被视为用户实现的属性在语法上的错误尝试。 You can't have no setter, but you can make the setter private: 您可以没有二传手,但可以将二传手设为私有:

public int AIP_PrivateSet { get; private set; }

A moment of derp. 一小会儿。

public int AIP_NoSet { get; set; }

Sounds like you want an automatic property with only a 'get' defined. 听起来您想要只定义了“ get”的自动属性。

This is not allowed by the compiler. 编译器不允许这样做。

You can accomplish this by adding a private set (as others have answered), or by not using an automatic property: 您可以通过添加一个private集(正如其他人回答的那样)或不使用自动属性来完成此操作:

private int _aip = int.MaxValue;
public int AIP_NoSet { get {return _aip;}}

Or, if you NEVER want to set it, just use a const: 或者,如果您永远不想设置它,只需使用const:

public const int AIP_NoSet = 2;

将setter访问权限设为私有并修复语法。

public int AIP_NoSet { get; private set; }

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

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