Sergey
Sergey

Reputation: 13

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?

Upvotes: 0

Views: 147

Answers (3)

user1228
user1228

Reputation:

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;

Upvotes: 2

James
James

Reputation: 5425

Make setter access private and fix syntax.

public int AIP_NoSet { get; private set; }

Upvotes: 2

user180326
user180326

Reputation:

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

Upvotes: 4

Related Questions