Domanda

public List<ObjectA> ObjectAList
{
    get
    {
        return ObjectAList ?? new List<ObjectA>();
    }

    set;
}

'ObjectAList.set' must declare a body because it is not marked abstract, extern, or partial

Why is that?

È stato utile?

Soluzione

You are trying to mix Auto implemented property with normal property. Moreover you need a backing field, otherwise you will run in to Stackoverflow exception. Your property declaration should be like:

private List<ObjectA> _ObjectAList; //private backing field
public List<ObjectA> ObjectAList
{
    get
    {
        return _ObjectAList ?? new List<ObjectA>();
    }

    set
    {
        _ObjectAList = value;
    }
}

Altri suggerimenti

Thats how the langauge works.

You cant do partial explicit / implicit.

Its all or nothing.

When you do it implicitly it just writes to an auto generated backing field, but it wants to do both sides. Moreover your get is going to have a stack overflow even if the set worked because you are going to call the get over and over with no base case to end the recursion.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top