Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实体类中公共访问器的私有变量。c#_C# - Fatal编程技术网

实体类中公共访问器的私有变量。c#

实体类中公共访问器的私有变量。c#,c#,C#,因此,我试图了解最佳实践,并将其与我所看到的和目前使用的进行比较。在下面的代码示例中,我们有一个实体类,没有类内方法。该类只是一个字段列表 /// <summary> /// AddUserID /// </summary> public string AddUserIDField = "Add_User_ID"; public string AddUserID { get { if (this.Row != null)

因此,我试图了解最佳实践,并将其与我所看到的和目前使用的进行比较。在下面的代码示例中,我们有一个实体类,没有类内方法。该类只是一个字段列表

/// <summary>
/// AddUserID
/// </summary>
public string AddUserIDField = "Add_User_ID";
public string AddUserID
{
    get
    {
        if (this.Row != null)
            return (string)mmType.GetNonNullableDbValue(this.Row["Add_User_ID"], 
                "System.String");
        else
            return this._AddUserID;
    }
    set
    {
        if (this.Row != null)
            this.Row["Add_User_ID"] = value;

        this._AddUserID = value;
    }
}

private string _AddUserID;
//
///AddUserID
/// 
公共字符串AddUserIDField=“添加用户ID”;
公共字符串AddUserID
{
收到
{
如果(this.Row!=null)
返回(字符串)mmType.GetNonNullableDbValue(此.Row[“添加用户ID”],
“System.String”);
其他的
返回此。\u AddUserID;
}
设置
{
如果(this.Row!=null)
此.Row[“添加用户ID”]=值;
这是。_AddUserID=value;
}
}
私有字符串_AddUserID;
私有字符串在这里有什么用途?它不在类本身中使用或访问。难道你不能将_AddUserID引用替换为AddUserID吗

这是我的公司框架,不是EF

非常感谢您的帮助

在C#中,当您编写
公共字符串MyVar{get;set;}
时,它实际上生成以下代码:

// The private variable holding the value of the property
private string _myVar;

// The getter and the setter of the property
public string MyVar {
    get
    {
        return _myVar;
    }
    set
    {
        _myVar = value;
    }
}
所以一个属性总是有一个底层的私有变量。该属性只是一种隐藏getter和setter的语法。它本身没有价值。大多数情况下,您可以在不编写相关私有变量的情况下编写属性,这正是C#编译器提供的syntaxic sugar(有关更多信息,请参阅)


在您向我们展示的代码中:由于代码使用特定的getter和setter,它必须显式地编写通常隐藏在属性后面的私有变量。

\u AddUserID
是您实际存储
AddUserID
引用的值的地方。如果您执行所要求的替换,则属性将以无限递归的方式不断调用自身。仅当您使用时。将
AddUserId
视为函数。似乎您应该将
行[“Add\u User\ID”]
引用替换为
行[AddUserIDField]
,除非该字段用于其他内容?简而言之:
\u AddUserID
是一个字段,&
AddUserID
是一个属性(带有getter和setter函数)。字段是一个声明为后端的变量,用于像Java一样存储属性值,但在C#case中,有自动属性自动声明getter和setter函数。