must declare a body because it is not marked abstract or extern? C#/ASP.NET

前端 未结 3 1339
走了就别回头了
走了就别回头了 2021-01-22 11:47

I have a normal web form with code behind, in this code behind I can instantiate a couple of classes I have in the root folder, such as:

public partial class _De         


        
相关标签:
3条回答
  • 2021-01-22 12:09
    public string id { get; set; }
    

    is an automatic property and these were added in C# v3.0 / .Net v3.5, so ensure you are targetting this version or higher in your project settings. If you wish to target an earlier version you'll need to use properties with a backing field.

    0 讨论(0)
  • 2021-01-22 12:10

    do you have TargetFramework = "3.5" for the project ? Check out this post.

    0 讨论(0)
  • 2021-01-22 12:24
    public string id { get; set;}
    

    This line is the issue, and the question has been answered above, but I am adding an aswer with code to illustrate how to put in backing fields for those who are new to C#. This an example of what the backing fields would look like, for public string id { get; set; }:

        private string p_id="";
        public string id {
            get {
                return p_id;
            }
            set {
                p_id = value;
            }
        }
    

    Note the initialized private string p_id that holds the actual value for the public string id. Also note that value is defined by the compiler, and is cast as a string in this instance. If the property id was an int instead, value would be cast as an int (integer) instead of a string.

    0 讨论(0)
提交回复
热议问题