开发者

.NET property generating “must declare a body because it is not marked abstract or extern”

I have checked the similar questions that the "questions with similar titles" result but they were all targeting .net 3.5 I am targeting 4.0 and I am getting must declare a body because it is not marked abstract,extern,or partial

Why am I getting this error and how can i prevent it without making my prope abstract,extern or partial? thank you very much开发者_StackOverflow社区

      public static SRDataContext DC
    {
        get
        {
            if (DC == null)
            {
                DC = new SRDataContext();
            }
            return DC;
        }
        private set
        {
            DC = value;
        }
    }


private static SRDataContext dc;

public static SRDataContext DC
{
    get
    {
        if (dc == null)
        {
            dc = new SRDataContext();
        }
        return dc;
    }
    private set
    {
        dc = value;
    }
}

You need a backing field for the property (a field where the data can be saved). In C# there are the auto properties, but they can't have a body. Their backing field is created "behind your back" by the C# compiler. So for example:

public static SRDataContext DC { get; private set; }

But it would be different from what you are trying to do.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜