声明是否在C#中分配内存或者是新的运算符分配内存? [英] Does declaration in c# allocate memory or is it the new operator that allocates memory?

查看:136
本文介绍了声明是否在C#中分配内存或者是新的运算符分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问声明在C#中的变量分配内存中创建或者是新的操作符分配内存,使调用构造函数初始化分配的变量在内存中?



据我了解,你不能调用任何类型的构造函数没有新的运营商。我对么?


解决方案

请问声明在C#中的变量分配内存中创建或者是新的运营商该分配内存,使调用教练来初始化内存分配的变量?




首先,让我们确保你问问题你认为你是在问。对于值类型,变量的存储位置和价值存储位置是的相同的存储的。对于引用类型,与之相关的存储位置的变量的包含的参考的与相关的存储位置的对象的。这些都是完全不同的。



其次,让我们澄清你由一个变量的申报的意思。静态字段,实例字段,局部变量和形式参数都声明。此外,局部变量和形式参数的配置语义是不同的,如果他们被封闭在一个lambda外当地人,和语义也有所不同,当本地处于异步方法或迭代器块。



因此,让我们假设你有引用类型的局部变量并没有什么花哨有关地方:

 无效M(){
动物X =新的长颈鹿(); ...



本地变量x的存储位置被分配掉短期储存的 - - 堆栈或寄存器,通常是 - 如果方法M()被激活。



在新长颈鹿()被评为运行时内存分配的GC堆 - - 在长期储存长颈鹿,然后通过一个引用该对象的构造。当构造函数返回,引用,然后分配给本地。



因此,有的两个的存储位置。有短期位置对于x,只要仅住作为该方法的激活,并有长期贮存即所指的事物,而住,直到垃圾收集清除它



如果不回答你的问题,澄清你的问题。




你能打电话没有新的运营商构造?




我假设的构造你的意思是实例构造函数,而不是一个静态构造函数。



不被任何正常的意思,没有。


Does declaration in c# allocate memory for the variable to be created or is it the new operator that allocates memory and enables to invoke the constructor to initialize the allocated variable in memory?

To my understanding, you cannot call an constructor of any type without the new operator. Am I correct?

解决方案

Does declaration in c# allocate memory for the variable to be created or is it the new operator that allocates memory and enables to invoke the instructor to initialize the allocated variable in memory?

First let's make sure that you're asking the question you think you're asking. For a value type, the variable storage location and the value storage location are the same storage. For a reference type, the storage location associated with the variable contains a reference to the storage location associated with the object. Those are completely different.

Second, let's clarify what you mean by "declaration" of a "variable". A static field, an instance field, a local variable and a formal parameter all have declarations. Moreover, the allocation semantics of local variables and formal parameters are different if they are closed-over outer locals of a lambda, and the semantics are also different when the local is in an async method or iterator block.

So let's assume that you have a local variable of reference type and there is nothing fancy about the local:

void M() { 
    Animal x = new Giraffe();  ...

The storage location for the local variable x is allocated off of the short-term storage -- the stack, or a register, typically -- when method M() is activated.

When the "new Giraffe()" is evaluated the runtime allocates memory for a Giraffe on the long-term storage -- the GC heap -- and then passes a reference to that object to the constructor. When the constructor returns, the reference is then assigned to the local.

So there are two storage locations. There's the short-term location for x, which only lives as long as the activation of the method, and there's the long-term storage for the thing that is referred to, and that lives until the garbage collector cleans it up.

If that doesn't answer your question, clarify your question.

Can you call a constructor without the new operator?

I'm assuming that by "constructor" you mean an instance constructor and not a static constructor.

Not by any "normal" means, no.

这篇关于声明是否在C#中分配内存或者是新的运算符分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆