【问题标题】:Stylecop issues SA1201 :Order of items in a classStylecop 发出 SA1201 : 类中项目的顺序
【发布时间】:2016-08-15 18:21:09
【问题描述】:

在 Stylecop 警告 SA1201 之后,我修改了类如下

/// <summary>
/// Class Data 
/// </summary>
public class DataClass
{
    /// <summary>
    ///  Gets or sets Id
    /// </summary>
    public string Id
    {
        get { return this.id; }
        set { this.id = value; }
    }

    /// <summary>
    ///  Gets or sets Name
    /// </summary>
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    /// <summary>
    /// Declare variable name
    /// </summary>
    private string name;

    /// <summary>
    /// Declare variable id
    /// </summary>
    private string id;
}

仍然显示相同的错误 “所有属性必须放在所有字段之后”

【问题讨论】:

  • 你搞错了。 idnameIdName 之前。

标签: c# asp.net stylecop


【解决方案1】:

我认为您混淆了属性和字段。属性使用 getter 和 setter,而字段是“传统”变量。

https://msdn.microsoft.com/library/x9fsa0sw.aspx

您的代码应如下所示:

/// <summary>
/// Class Data 
/// </summary>
public class DataClass
{    
    /// <summary>
    /// Declare variable name
    /// </summary>
    private string name;

    /// <summary>
    /// Declare variable id
    /// </summary>
    private string id;

    /// <summary>
    ///  Gets or sets Id
    /// </summary>
    public string Id
    {
        get { return this.id; }
        set { this.id = value; }
    }

    /// <summary>
    ///  Gets or sets Name
    /// </summary>
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2018-12-28
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多