C#问题私人变量

C#问题私人变量,c#,variables,private,C#,Variables,Private,只是想确定一下 public class Product { private decimal unitPrice; public int Id { get; set; } public string Code { get; set; } public string Name { get; set; } //private string code; public decimal Unitp

只是想确定一下

public class Product { private decimal unitPrice; public int Id { get; set; } public string Code { get; set; } public string Name { get; set; } //private string code; public decimal Unitprice { get { return unitPrice; } set { if (value >=0) unitPrice = value; } } } 公共类产品 { 私人十进制单价; 公共int Id{get;set;} 公共字符串代码{get;set;} 公共字符串名称{get;set;} //私有字符串码; 公共十进制单价 { 得到 { 退货单价; } 设置 { 如果(值>=0) 单价=价值; } } }
为什么我们必须将私有变量设置为unitPrice才能将值返回到unitPrice,这是出于某种原因编写的吗?

您不必将其设置为私有变量,而只是为了返回值
private
是此处的
access修饰符之一。它们用于限制代码中特定变量的访问/使用范围

private
这里的意思是
unitPrice
当前可访问或只能由该特定类别使用。没有其他外部程序集可以使用此变量

如果要访问外部其他区域中的变量,可以选择将其
公开


希望这会清除它。

您不会为了返回它的值而将其设置为私有
private
是此处的
access修饰符之一。它们用于限制代码中特定变量的访问/使用范围

private
这里的意思是
unitPrice
当前可访问或只能由该特定类别使用。没有其他外部程序集可以使用此变量

如果要访问外部其他区域中的变量,可以选择将其
公开


希望这会清除它。

您不会为了返回它的值而将其设置为私有
private
是此处的
access修饰符之一。它们用于限制代码中特定变量的访问/使用范围

private
这里的意思是
unitPrice
当前可访问或只能由该特定类别使用。没有其他外部程序集可以使用此变量

如果要访问外部其他区域中的变量,可以选择将其
公开


希望这会清除它。

您不会为了返回它的值而将其设置为私有
private
是此处的
access修饰符之一。它们用于限制代码中特定变量的访问/使用范围

private
这里的意思是
unitPrice
当前可访问或只能由该特定类别使用。没有其他外部程序集可以使用此变量

如果要访问外部其他区域中的变量,可以选择将其
公开


希望这能澄清问题。

从设计角度来看,单价属性与其他属性完全相同,但有一个约束条件

 if (value >=0)
只有正价格是有效的,你别无选择,只能预见一个私有变量,而这个私有变量是房地产的基础

在早期版本的.NET中,我们无法执行以下操作:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}
而且必须一直完整地写出来。上面的符号更方便,代码也更清晰。过去是这样的:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}

从设计角度来看,单价属性与其他属性完全相同,但这是因为它受到限制

 if (value >=0)
只有正价格是有效的,你别无选择,只能预见一个私有变量,而这个私有变量是房地产的基础

在早期版本的.NET中,我们无法执行以下操作:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}
而且必须一直完整地写出来。上面的符号更方便,代码也更清晰。过去是这样的:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}

从设计角度来看,单价属性与其他属性完全相同,但这是因为它受到限制

 if (value >=0)
只有正价格是有效的,你别无选择,只能预见一个私有变量,而这个私有变量是房地产的基础

在早期版本的.NET中,我们无法执行以下操作:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}
而且必须一直完整地写出来。上面的符号更方便,代码也更清晰。过去是这样的:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}

从设计角度来看,单价属性与其他属性完全相同,但这是因为它受到限制

 if (value >=0)
只有正价格是有效的,你别无选择,只能预见一个私有变量,而这个私有变量是房地产的基础

在早期版本的.NET中,我们无法执行以下操作:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}
而且必须一直完整地写出来。上面的符号更方便,代码也更清晰。过去是这样的:

public int Id { get; set; }
private int _id;

public int Id{
     get{ return _id;}
     set{ _id = value;}
}

公共财产单价向其他类别公开,以便读取或修改。但是作为属性允许类对值进行一定程度的控制——例如,您可以验证任何更改。此外,在内部,您可以完全更改获取和修改值的方式,而无需更改类的约定。换句话说,您可以在不影响任何消费者的情况下进行此类更改

支持字段unitPrice纯粹是一个实现细节,它是由类封装的内部状态。公开此字段意味着您将失去任何和所有修改值派生的机会,并且您没有验证值更改的入口点。属性一开始看起来似乎毫无意义,但它们将帮助您的代码更易于维护,因为更改可以更容易地限制在单个类中

get和set块纯粹是语法上的糖类,编译器在幕后创建了两个方法,分别称为get_UnitPrice()和set_UnitPrice(x),用于对属性进行读/写操作。您可以使用类似的方法,但专业