简体   繁体   English

在C#中,Properties在技术上被认为是非静态的吗?

[英]Are Properties technically considered nonstatic in C#?

My textbook is referring to the this reference and it first implies that a property is somewhat static and does not store one with each object, but one for the entire class. 我的教科书是指这个引用,它首先暗示一个属性有点静态,并且不会为每个对象存储一个属性,而是为整个类存储一个属性。 Then later it says that a property is nonstatic. 后来它说房产是非静态的。 I am really confused. 我真的很困惑。 What is it? 它是什么?

  • Properties can be static or not static. 属性可以是静态的但不是静态的。
  • Static properties have the 'static' keyword, default is not static. 静态属性具有'static'关键字,默认值不是static。
  • Properties that are static are stored for the entire class (there is only one). 静态属性存储在整个类中(只有一个)。
  • Properties that are not static are stored per instance. 每个实例都存储非静态属性。

A property can be both static and non-static you decide which by using the static keyword. 通过使用static关键字,您可以决定静态和非静态属性。

public static int StaticProperty {get; set; }
public int InstanceProperty {get; set; }

On a side note, a property is actually two methods (or just one if you only implement the set or get ). 另一方面,属性实际上是两种方法(如果只实现setget则只有一种方法)。

public int MyProperty {get; set; }

is equivalent to 相当于

public void set_MyProperty(int value);
public int get_MyProperty();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 非静态字段需要对象引用 - C# An object reference is required for the nonstatic field 使用C#的get set属性被认为是好习惯吗? - Is using get set properties of C# considered good practice? C#Unity“字段初始化器无法引用非静态字段,方法...” - C# Unity “A Field Initializer cannot reference the nonstatic field, method…” 使用C#在Windows 7上为工作组剪贴板重新创建Windows在技术上会困难吗? - Would it be technically difficult to recreate windows for workgroup clipboard on windows 7 with c#? 在C#中,正在通过this访问当前对象的属性。与仅仅XYZ相比,XYZ被认为是较差的样式 - In C#, is accessing the current object's properties through this.XYZ considered poor style compared to just XYZ 对象初始值设定项中的属性赋值与C#6中的自动属性不在同一级别上 - Property assignments in object initializers not considered on the same level as auto-properties in C# 6 字段初始化程序无法引用C#中的非静态字段方法或属性错误 - A field initializer cannot reference the nonstatic field method or property error in c# C#相等字符串不被认为相等 - c# equal strings not considered equal 为什么“文件”在 C# 中被视为关键字? - Why is "file" considered a keyword in C#? "C# 是否被视为上下文无关语言?" - Is C# considered a context free language?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM