开发者

Auto-Implemented Properties Error

开发者 https://www.devze.com 2023-02-07 21:37 出处:网络
I try to use AIP public int AIP_NoSet { get { ;} } Compiler say that it is an error: Program.c1.AIP_NoSet.get\': not all code 开发者_StackOverflow中文版paths return a value

I try to use AIP

public int AIP_NoSet
{
    get { ;}
}

Compiler say that it is an error:

Program.c1.AIP_NoSet.get': not all code 开发者_StackOverflow中文版paths return a value

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.

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 int _aip = int.MaxValue;
public int AIP_NoSet { get {return _aip;}}

Or, if you NEVER want to set it, just use a const:

public const int AIP_NoSet = 2;


Make setter access private and fix syntax.

public int AIP_NoSet { get; private set; }
0

精彩评论

暂无评论...
验证码 换一张
取 消