通过副本或参考传递?

发布于 2024-09-16 01:58:58 字数 114 浏览 20 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的背包 2024-09-23 01:58:58

在 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 the struct 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 the class 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 from System.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.

说谎友 2024-09-23 01:58:58

默认情况下,所有类型均按值传递。 值类型 (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

北陌 2024-09-23 01:58:58

一般来说,

  • 值类型,包括本机类型(intfloat...)以及struct 是“通过复制”传递的(如您所说),而在

  • 引用类型 中,其中包括 classes,仅复制引用而不是完整的对象。

但请注意,字符串不是“通过复制”传递的!它是一种引用类型,但由于它是不可变的,它的行为类似于值类型

请注意,ref 关键字在这两种情况下都很有用:对于值类型,这意味着将值复制回来。对于引用类型,这意味着引用可以更改(即,您可以将变量分配给新对象,而不仅仅是更改对象的属性。)

In general,

  • value types, which includes native types (int, float, ...) as well as structs are passed "by copy" (as you put it), whereas in

  • reference types, which includes classes, 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.)

烙印 2024-09-23 01:58:58

另请注意,结构对象可以包含引用类型:

struct Currency
{
    int Amount {get;set;} // is referenced by value (copy)
    string Code {get;set;} // is referenced by reference
}

当您像这样使用该结构时:

var obj1 = new Currency{Amount = 1, Code = "USD"};
var obj2 = obj1;

那么

object.ReferenceEquals(obj1.Code, obj2.Code); // false - string is reference type, but used inside value type

如果您已将Currency声明为类,则对Code的引用是相同的。

Note also that struct objects can include reference types:

struct Currency
{
    int Amount {get;set;} // is referenced by value (copy)
    string Code {get;set;} // is referenced by reference
}

When your use that struct like this:

var obj1 = new Currency{Amount = 1, Code = "USD"};
var obj2 = obj1;

Then

object.ReferenceEquals(obj1.Code, obj2.Code); // false - string is reference type, but used inside value type

If you have declared Currency as class then references to Code were the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文