我在我的代码中使用"自动"属性,我想知道这段代码之间的实际区别是什么:
public class foo{
public int i;
}
Run Code Online (Sandbox Code Playgroud)
和
public class foo{
public int i {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我知道存在差异,因为我使用的正常第三方错过了公众成员,但发现他们曾经添加过{get; set;}.
由于背后没有私人领域,幕后背后会发生什么?
为对象定义属性而不是直接访问私有变量有什么好处?
代替 :
public class A
private _x as integer = 0
Public property X() as integer
Get
return _x
End Get
Set(ByVal value As integer)
_x = value
End Set
End Property
end class
Run Code Online (Sandbox Code Playgroud)
为什么我们不能做以下事情:
public class A
public _x as integer = 0
end class
Run Code Online (Sandbox Code Playgroud)
有什么好处?
所以我知道类和结构是数据的结构.类字段默认为私有字段和结构字段 - 公共字段.喜欢public: / int a;C++和public int a;C#
但是访问这些字段的其他方法是将它们设为私有并使用函数/方法.喜欢SetValue(int value){a = value;}和/ GetValue() { return a; }或者我甚至听说过{set; get;}C#的新酷.
但为什么呢?许多人告诉我'其他人可以通过这种方式访问你的变量,所以让他们私有化'.我不明白,它有什么区别让它们公开,只是使用它们obj.a = 3;或让它们变得私密和干嘛obj.SetValue(3);?有人(甚至可以简要地)解释一下这些差异是什么以及如何在他们公开的时候进入这些领域?
可能是一个重复的问题.我确实搜索了这个并引用了这些文章
我明白的一点是,
我真正想要清楚的是,
public class Employee {
public string strName;
}
public class Employee {
public string strName {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
UPDATE
我知道它是一个重复的问题,我提到了它.请问我问的第二点问题.答案究竟是什么?我无法理解.
如果我没有任何逻辑来设置该值,那么它是一个好的实践还是需要什么?
好的,谢谢大家的回复.我现在明白了.因为我很新,我无法掌握它.但现在我明白了.抱歉浪费你所有的时间.
考虑以下两个选项:
public int Foo { get; set; }
public int Foo;
Run Code Online (Sandbox Code Playgroud)
它们似乎在语义上是等价的,我相信它们甚至会编译成同一个IL.那么使用该物业有什么好处?看到一个公共领域让我感到不安,但我想不出使用属性语法的任何具体优势.如果将来需要显式的getter和setter,public int Foo;则可以替换为public int Foo { ... }不需要其他更改.我能想到的最好的是属性语法感觉更好,但我很难用这个理由来说服其他人.
在这种情况下使用属性语法的优点(如果有的话)是什么?
可能的重复:
公共字段与自动属性
我认为这个问题会在某个地方得到解答,但我在通常的地方找不到它。我想知道这样做有什么好处
private int _foo;
public int foo {get {return _foo;} set{_foo = value;}}
Run Code Online (Sandbox Code Playgroud)
或者
public int foo {get; set;}
Run Code Online (Sandbox Code Playgroud)
刚刚超过
public int foo;
Run Code Online (Sandbox Code Playgroud)
如果需要更复杂的操作,我可以看到好处,但是对于像这样的简单情况有什么好处呢?
有人告诉我你可以替换以下代码:
private string name;
public string Name
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
以下内容并没有任何不良影响:
public string Name;
Run Code Online (Sandbox Code Playgroud)
我意识到,像第一个例子中那样设置的属性与我删除它并设置原始属性时的属性几乎相同,public但是对于你需要的属性的第二种方式是不好的编程习惯基本的吸气剂和二传手?
我正在尝试在列表之间创建一个检查,但我没有运气:-/
我有一个包含 100 个字段的游戏板,并进行此循环以仅将空字段添加到新列表中:
for(int i = 0; i < thisGame.boardFields.Count; i++)
{
if (thisGame.boardFields.Count != 0 && thisGame.boardFields [i] != null)
{
BoardField thisField = thisGame.boardFields [i];
if (thisField.owner == "0" && thisField.number != 13)
{
Tile tTile = new Tile();
tTile.color = thisField.color;
tTile.number = thisField.number.ToString();
tempBoard.Add (tTile);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我遍历玩家 5 块瓷砖,看看玩家是否有一块不可玩的瓷砖,例如,具有相同对象的空字段不可用,如下所示:
for (var i = 0; i < thisGame.playerTiles.Count; i++)
{
Tile tempTile = new Tile();
tempTile.color = thisGame.playerTiles[i].color;
tempTile.number = thisGame.playerTiles[i].number.ToString();
if (!tempBoard.Contains (tempTile)) …Run Code Online (Sandbox Code Playgroud) 我正在询问使用 getter 和 setter 的建议。我在两个版本中编写了相同的代码:使用 getter 和 setter 以及仅使用类方法。而且我看不出它们之间的区别。
我用私有字段评级编写了类 Book。并且构造函数 Book 可以通过 RatingSetter 或 RatingMethod 为 Book.rating 分配一些东西。RatingMethod 只设置值,但我也可以创建一个仅用于获取值的方法。
class Book
{
public string title;
public string author;
private string rating;
public Book(string title, string author, string rating)
{
this.title = title;
this.author = author;
RatingSetter = rating;
RatingMethod(rating);
}
public string RatingSetter
{
get { return this.rating; }
set
{
if (value == "PG" || value == "PG-13" || value == "R")
{
rating = value;
}
else …Run Code Online (Sandbox Code Playgroud) 我正在创建一个简单的User类,如果我使用私有字段的公共属性只使用公共字段,这是否重要?
这是我的意思的一个例子:
public class clsUser
{
private string name;
private string lName;
public string Name
{
get
{
return name;
}
set
{
name= value;
}
}
public string LName
{
get
{
return lName;
}
set
{
lName= value;
}
}
public clsUser(string userID)
{
//get the user id here and set the properties
this.name= getName(userID);
this.lName= getLName(userID);
}
}
Run Code Online (Sandbox Code Playgroud)
或者我可以做
public string name;
public string lName;
Run Code Online (Sandbox Code Playgroud)
public 现在担心输入所有这些:
public string Name
{
get
{
return name;
}
set …Run Code Online (Sandbox Code Playgroud)