通过副本或参考传递?
我对 C# 有点陌生,只是想知道是否有一个默认作为副本传递的类列表(常用)。我怎样才能识别它们?
我知道基本的基本对象类型——int、uint、float、strings……——都是通过复制传递的。
I am a bit new to C# and was just wondering if there is a list of classes (commonly used) which are by default passed as copy. How can I identify them?
I know the basic basic object types — int, uint, float, strings, ... — are passed by copy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C# / .Net 中,对象可以分为值类型或引用类型 [1]。值类型是从
System.ValueType
派生并在 C# 中使用struct
类型声明定义的任何类型。这些通过副本/值传递。引用类型不是从
System.ValueType
派生的类型,而是在 C# 中使用class
关键字定义的类型。引用类型实例的标识符称为引用(类似于指针)。默认情况下,它们也是按值传递的,但仅传递引用而不是整个对象。您的问题还提到
string
实例是通过副本传递的。 .Net 中的String
是一种引用类型(直接从System.Object
派生),因此不会通过完整副本传递。[1] 指针在这里可能值得拥有自己的类,但我在本次讨论中忽略它们。
In C# / .Net objects can either be classified as value or reference types [1]. Value types are any type which derive from
System.ValueType
and are defined in C# with thestruct
type declaration. These are passed by copy / value.Reference types are types which do not derive from
System.ValueType
and are defined in C# with theclass
keyword. Identifiers to instances of reference types are known as references (similar to pointers). These are also passed by value by default but only the reference is passed not the whole object.Your question also mentioned that
string
instances are passed by copy.String
in .Net is a reference type (derives directly fromSystem.Object
) and hence is not passed by full copy.[1] Pointers may merit their own class here but I'm ignoring them for this discussion.
默认情况下,所有类型均按值传递。 值类型 (
struct
) 和引用类型 (class
) 之间的区别在于,对于值类型, >值的副本被传递给方法,而对于引用类型,仅传递引用。请参阅 MSDN 了解更多详细信息。
另外,不要混淆值/引用类型的概念以及按值或按引用传递参数的概念。有关详细信息,请参阅 Jon Skeet 撰写的这篇文章
All types, by default, are passed by value. The difference between value types (
struct
) and reference types (class
) is that for value types a copy of the value is passed to the method, whereas for reference types, only a reference is passed.See MSDN for more details.
Also, don't confuse the notion of value/reference types, and the notion of passing parameters by value or by reference. See this article by Jon Skeet for details
一般来说,
值类型,包括本机类型(
int
、float
...)以及struct
是“通过复制”传递的(如您所说),而在引用类型 中,其中包括
class
es,仅复制引用而不是完整的对象。但请注意,字符串不是“通过复制”传递的!它是一种引用类型,但由于它是不可变的,它的行为类似于值类型。
请注意,
ref
关键字在这两种情况下都很有用:对于值类型,这意味着将值复制回来。对于引用类型,这意味着引用可以更改(即,您可以将变量分配给新对象,而不仅仅是更改对象的属性。)In general,
value types, which includes native types (
int
,float
, ...) as well asstruct
s are passed "by copy" (as you put it), whereas inreference types, which includes
class
es, only a reference is copied instead of the complete object.Note, though, that string is not passed "by copy"! It's a reference type, but since it's immutable, it behaves similar to a value type.
Note that the
ref
keyword can be useful in both cases: In the case of value types, it means that the value is copied back. In the case of reference types, it means that the reference can be changed (i.e., that you can assign the variable to a new object rather than just changing the properties of the object.)另请注意,结构对象可以包含引用类型:
当您像这样使用该结构时:
那么
如果您已将Currency声明为类,则对Code的引用是相同的。
Note also that struct objects can include reference types:
When your use that struct like this:
Then
If you have declared Currency as class then references to Code were the same.