C# 中什么是值类,什么是引用类?

发布于 2024-08-10 06:29:22 字数 579 浏览 4 评论 0原文

C# 中值类引用类 的定义是什么?

这与值类型引用类型有何不同?

我问这个问题是因为我在 MCTS 自定进度培训套件(考试 70-536)中读到了这一点。第 1 章、第 1 课、第 4 课复习:

您需要创建一个简单的类或 只包含值的结构 类型。您必须创建该类或 结构,使其运行为 尽可能高效。你一定是 能够将类或结构传递给 一个程序,无需担心 程序将对其进行修改。哪一个 您应该创建以下内容吗?

参考类

B参考结构

C值等级

D值结构

正确答案:D

错误:您可以创建一个 参考类;然而,它可能是 当传递给过程时进行修改。

B 错误:您无法创建 参考结构。

C 错误:你可以创造一个值 班级;然而,结构往往是 效率更高。

D 正确:值结构是 通常是最有效的。

What is the definition of a value class and reference class in C#?

How does this differ from a value type and reference type?

I ask this question because I read this in the MCTS Self-Paced Training Kit (Exam 70-536). Chapter 1, Lesson 1, Lesson review 4 :

You need to create a simple class or
structure that contains only value
types. You must create the class or
structure so that it runs as
efficiently as possible. You must be
able to pass the class or structure to
a procedure without concern that the
procedure will modify it. Which of the
following should you create?

A reference class

B reference structure

C value class

D value structure

Correct Answer: D

A Incorrect: You could create a
reference class; however, it could be
modified when passed to a procedure.

B Incorrect: You cannot create a
reference structure.

C Incorrect: You could create a value
class; however, structures tend to be
more efficient.

D Correct: Value structures are
typically the most efficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

影子是时光的心 2024-08-17 06:29:23

值类型存储实际数据,而引用类型存储对数据的引用。引用类型动态存储在堆上,而值类型存储在堆栈上。

值类型: http://msdn.microsoft.com/en-us/library /s1ax56ch.aspx
参考类型: http://msdn.microsoft.com/en-us/library /490f96s2.aspx

Value types store the actual data while reference types store references to the data. Reference types are stored dynamically on the heap while value types are stored on the stack.

Value Types: http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx
Reference Types: http://msdn.microsoft.com/en-us/library/490f96s2.aspx

苍风燃霜 2024-08-17 06:29:23

当您引用值类型(即通过使用其名称)时,您正在谈论数据在内存中的位置。因此,值类型不能为 null,因为内存位置无法表示“我不代表任何东西”。默认情况下,您按值传递值类型(即,传递给方法的对象不会因方法的执行而改变)。

当您使用引用类型对象时,您实际上是在变相使用指针。该名称引用一个内存位置,然后引用该对象实际所在的内存位置。因此,您可以将 null 分配给引用类型,因为它们有一种表达“我没有指向任何地方”的方式。引用类型还允许对象因方法执行而更改,因此您可以通过将 myReferenceObject 传递到方法调用来更改其属性。

When you refer to a value type (that is, by using its name), you're talking about the place in memory where the data is. As such, value types can't be null because there's no way for the memory location to say "I don't represent anything." By default, you pass value types by value (that is, the object you pass in to methods doesn't change as a result of the method's execution).

When you use a reference type object, you're actually using a pointer in disguise. The name refers to a memory location, which then references a place in memory where the object actually lives. Hence you can assign null to a reference type, because they have a way of saying "I point to nowhere." Reference types also allow the object to be changed as a result of methods executing, so you can change myReferenceObject's properties by passing it into a method call.

家住魔仙堡 2024-08-17 06:29:23

引用类型通过引用传递给方法,值类型通过值传递给方法;在后一种情况下,方法接收变量的副本,而在前一种情况下,方法接收对原始数据的引用。如果您更改副本,原件不会更改。如果更改所引用的原始数据,则在数据引用发生更改的任何地方,数据都会发生更改。如果用 C 创建与 C# 程序类似的程序,通常引用类型就像使用指针的数据,而值类型将是堆栈上的普通数据。

数字类型、字符、日期、枚举和结构都是值类型。字符串、数组、委托和类(实际上是大多数东西)都是引用类型。

Reference types are passed to methods by reference and value types by value; in the latter case a method receives a copy of the variable and in the former it receives a reference to the original data. If you change your copy, the original does not change. If you change the original data you have a reference to, the data changes everywhere a reference to the data is changed. If a similar program to your C# program was created in C, generally reference types would be like data using pointers and value types would be normal data on the stack.

Numeric types, char, date, enumerations, and structures are all value types. Strings, arrays, delegates and classes (i.e., most things, really) are reference types.

[旋木] 2024-08-17 06:29:23

如果我的理解是正确的,您可以通过使用通过构造函数初始化的只读成员变量来完成“值类”或不可变类。一旦创建,这些就无法更改。

If my understanding is correct, you can accomplish a "value class", or immutable class, through the use of readonly member variables initialized through the constructor. Once created, these cannot be changed.

过去的过去 2024-08-17 06:29:22

您可能会想到 C++/CLI,与 C# 不同,它允许用户声明“值类”或“引用类”。
在 C# 中,您声明的任何类都将隐式地成为引用类 - 只有内置类型、结构和枚举才具有值语义。
要了解 C++/CLI 中的值类,请查看此处:
http://www.ddj.com/cpp/184401955

相比之下,值类的功能非常少引用类,对于“普通旧数据”很有用;也就是说,没有身份的数据。由于在将一个对象分配给另一个对象时要复制数据,因此系统为您提供了一个默认(且强制)的复制构造函数,该构造函数只需将数据复制到另一个对象。

要将值类转换为引用类(从而将其放入垃圾收集堆中),您可以将其“装箱”。

要确定您正在编写的类是其中之一,请问问自己它是否具有标识。这通常意味着它具有某种状态,或者具有标识符或名称,或者其自身上下文的概念(例如指向附近节点的节点)。

如果没有,它可能是一个值类。

然而,在 C# 中,值类被声明为“结构”。

You may be thinking of C++/CLI which, unlike C#, allows the user to declare a "value class" or a "ref class."
In C#, any class you declare will implicitly be a reference class - only built-in types, structs, and enums have value semantics.
To read about value class in C++/CLI, look here:
http://www.ddj.com/cpp/184401955

Value classes have very little functionality compared to ref classes, and are useful for "plain old data"; that is, data which has no identity. Since you're copying the data when you assign one to another, the system provides you with a default (and mandatory) copy constructor which simply copies the data over to the other object.

To convert a value class into a reference class (thereby putting it on the garbage-collected heap) you can "box" it.

To decide whether a class you are writing is one or the other, ask yourself whether it has an identity. That usually means that it has some state, or has an identifier or a name, or a notion of its own context (for example a node pointing to nearby nodes).

If it doesn't, it's probably a value class.

In C#, however, value classes are declared as "structs".

请你别敷衍 2024-08-17 06:29:22

请参阅有关该主题的概述,但请认真关注 msnd 链接和阅读其完整的通用类型系统章节。 (您也可以在第一个问题的评论中提问)

See the overview on the subject, but seriously follow the msnd links and read the full Common Type system chapter of it. (You could also have asked in a comment in the first, question)

↘人皮目录ツ 2024-08-17 06:29:22

值类型通过值传递,而引用类型通过引用传递。

编辑:值/参考类
C# 中没有“值类”或“引用类”的概念,因此询问其定义是没有意义的。

Value types are passed by value, while reference types are passed by reference.

Edit: value/reference classes
There is no concept of a 'value class' or 'reference class' in C#, so asking for its definition is moot.

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