简体   繁体   中英

Auto-Implemented Properties Error

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 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;

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

public int AIP_NoSet { get; private set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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