Guid 被视为值类型还是引用类型?

发布于 2024-08-23 06:57:34 字数 120 浏览 2 评论 0原文

指南是使用 new 关键字创建的,这让我认为它是引用类型。

这是正确的吗?

Guid uid = new Guid();

Guid 是否存储在堆上?

Guids are created using the new keyword which makes me think it's a reference type.

Is this correct?

Guid uid = new Guid();

Are Guids stored on the heap?

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

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

发布评论

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

评论(6

旧时模样 2024-08-30 06:57:34

Guid 是一种值类型。

请参阅 MSDN。请注意,Guid 是一个结构。所有结构都是值类型

Guid is a Value Type.

See MSDN. Note that Guid is a struct. All structs are Value Types.

━╋う一瞬間旳綻放 2024-08-30 06:57:34

你可以自己看看Guid的定义:

public struct Guid ...

或者你可以这样测试:

bool guidIsValueType = typeof(Guid).IsValueType;

引用:“GUID 是使用 new 关键字创建的,这让我认为它是一个引用类型。”

结构也可以有构造函数,例如 new DateTime(2012, 12, 23)

You can see the definition of a Guid yourself:

public struct Guid ...

Or you can test it like this:

bool guidIsValueType = typeof(Guid).IsValueType;

Quote: "GUID's are created using the new keyword which makes me think it's a reference type."

Structs can have constructors too, for example new DateTime(2012, 12, 23).

泪冰清 2024-08-30 06:57:34

GUID 是使用 new 关键字创建的,这让我认为它是一个引用类型。

别再这么想了。值类型也可以有构造函数。虽然很奇怪,但说

int x = new int();

这与将零分配给 x 是完全合法的。

这是正确的吗?

没有。

GUID 是否存储在堆上?

是的。指南也存储在堆栈中。

请注意,下面的分析假设 CLI 的实现是在 Windows 上运行的 Microsoft“桌面”或“Silverlight”CLR。我不知道其他版本的 CLI 的作用是什么,它们在 Mac 上的作用等等。如果您需要知道特定的内存块是否存储在其他实现中的堆栈上,则必须询问这些实现方面的专家。

Guid 在以下情况下存储在堆栈中:

(1) 当 Guid 是正在进行的计算的“临时”结果或用作方法的参数时。例如,如果您有一个方法调用 M(new Guid()),则在堆栈上分配新 Guid 的临时存储空间。

(2) 当 Guid 是局部变量时,该局部变量 (a) 不在迭代器块中,(b) 不是匿名方法或 lambda 表达式的封闭外部变量。

在所有其他情况下,Guid 不存储在堆栈中。当 Guid 是引用类型的字段、数组的元素、匿名方法或 lambda 表达式的封闭局部变量或者迭代器块中的局部变量时,Guid 会存储在堆上。

Guid 也可能既不存储在 GC 堆中,也不存储在堆栈中。 Guid 可能存储在完全非托管内存中,通过不安全的指针算术访问。

我很好奇为什么你如此关心 guid 的位是在堆栈上还是在堆上。这有什么区别呢?

GUID's are created using the new keyword which makes me think it's a reference type.

Stop thinking that. Value types can have constructors too. It is perfectly legal, though strange, to say

int x = new int();

That's the same as assigning zero to x.

Is this correct?

Nope.

Are GUID's stored on heap?

Yes. Guids are also stored on the stack.

Note that the analysis below assumes that the implementation of the CLI is the Microsoft "desktop" or "Silverlight" CLR running on Windows. I have no idea what other versions of the CLI do, what they do on Macs, and so on. If you need to know whether a particular hunk of memory is stored on the stack in other implementations, you'll have to ask someone who is an expert on those implementations.

A Guid is stored on the stack under the following circumstances:

(1) when the Guid is a "temporary" result of an ongoing calculation or is being used as an argument to a method. For example, if you have a method call M(new Guid()) then temporary storage for the new Guid is allocated on the stack.

(2) when the Guid is a local variable which is (a) not in an iterator block, (b) not a closed-over outer variable of an anonymous method or lambda expression.

In all other situations the Guid is not stored on the stack. A Guid is stored on the heap when it is a field of a reference type, an element of an array, a closed-over local of an anonymous method or lambda expression, or a local in an iterator block.

A Guid may also be stored in neither the GC heap nor the stack. A Guid might be stored in entirely unmanaged memory, accessed via unsafe pointer arithmetic.

I am curious as to why you care so much as to whether the bits of a guid are on the stack or on the heap. What difference does it make?

苍白女子 2024-08-30 06:57:34

它实际上是 Guid。所有类型都是使用 new 关键字构造的。您可以根据值类型是否为来识别引用类型和值类型, 界面,或委托(所有引用类型),或 结构枚举(值类型)。

It's actually Guid. All types are constructed using the new keyword. You can identify reference types from value types by whether they are a class, interface, or delegate (all reference types), or a struct or enum (value types).

蛮可爱 2024-08-30 06:57:34

它的值类型,见下面的例子:

using System;                   
public class Program
{
    public static void Main()
    {
        Guid a1 = new Guid();
        Console.WriteLine(a1);
        Guid b1 = a1;
        Console.WriteLine(b1);
        a1 = Guid.NewGuid();
        Console.WriteLine(a1);
        Console.WriteLine(b1);
    }
}

/* OUTPUT
00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
164f599e-d42d-4d97-b390-387e8a80a328
00000000-0000-0000-0000-000000000000
*/

Its value type, See the below example:

using System;                   
public class Program
{
    public static void Main()
    {
        Guid a1 = new Guid();
        Console.WriteLine(a1);
        Guid b1 = a1;
        Console.WriteLine(b1);
        a1 = Guid.NewGuid();
        Console.WriteLine(a1);
        Console.WriteLine(b1);
    }
}

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