创建一个实例 [英] Creating an Instance

查看:134
本文介绍了创建一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



什么是为类创建引用和创建对象?

演示类D;
-C#编译器通过this时会发生什么,其中Democlass是一个类?

Democlass D = new Democlass();
-当编译器通过此语句时,幕后将发生什么?


请帮帮我.

问候
Chaithanya M

解决方案

这是最粗略的解释,由于我不是老师,所以我对自己是否愿意投入精力搞定".

 DemoClass D; 



这仅定义与对象D关联的类型和范围,以便编译器知道在代码中其他位置对该对象的引用时应允许哪些内容.

演示类D =  Democlass(); 



当您调用new时,将为可能声明的所有属性分配内存,并为该对象返回一个指针.这称为实例化".

这是非常基础的编程知识,您最好在google上找到初学者的参考文献.到"Democlass"类型的对象或从其派生的类.将空值赋给"D"-它什么都没有引用.

Democlass D = new Democlass();

与上一个示例完全相同,除了在托管堆上创建DemoClass实例并将其分配给"D".

最大的区别在于,在第一个示例中,尝试使用D.Property或D.Method()会导致引发异常,第二个示例将起作用.


现在,考虑者D有一些参考..它将(变量D)存储在堆栈还是堆中?"


取决于声明它的位置.
如果在方法中:堆栈
如果在类中:堆(因为该类是引用类型并且在堆上进行,所以它的内容在堆上进行)
如果它在Struct:堆栈中(因为它是一个值类型,它们就放在堆栈中)


添加到Griff的答案:

变量可以存储在三个位置:堆栈,堆和进程的静态内存区域(建议保持最小).对于引用类型,变量(引用本身)可以存储在所有三个位置,而引用所指向的内存区域始终是堆(排除为C ++/CLI,由于对引用类型的唯一value semantic,此限制已被删除).结构是一个值类型,以及枚举和原始类型,所有其他类型都是引用类型.

为简单起见,我不会在下面提及堆,因此请记住该引用指向堆.

现在,它取决于声明变量的位置:如果变量是静态的,则将其存储在静态内存区域中.如果变量是局部变量,则始终存储在堆栈中.引用类型的字段始终进入堆栈.

所有这些规则可以组合在一起,因为结构可以具有引用类型字段和引用类型静态字段,值类型字段和值类型静态字段;和一个类可以具有相同的字段组合.自己找出所有可能的结构.

—SA


Hi ,

What is creating Reference and Creating object for a class?

Democlass D;
--what happens when C# complier passes through this ,where Democlass is a class?

Democlass D =new Democlass();
--What happens behind the scenes when complier passes through this statement ?


Kindly,help me out .

Regards
Chaithanya M

解决方案

This is the most cursory of explanations, and since I''m not a teacher, I''m not emotionally invested in whether or not you "get it".

DemoClass D;



This merely defines the type and scope associated with the object D so that the compiler knows what to allow disallow with regards to references to that objecft elsewhere in the code.

Democlass D =new Democlass();



When you call new, memory is allocated for any properties that might be declared, and a pointer is returned for the object. This is called "instantiation".

This is pretty basic programing stuff, and you would be better served to find a beginner''s reference on google.


Democlass D;

Creates a variable called "D" that can hold a reference to an object of type "Democlass", or a class derived from it. Assigns null to "D" - it refers to nothing.

Democlass D = new Democlass();

Does exactly the same as the previous example, except that an instance of DemoClass is created on the managed heap and assigned to "D".

The big difference is that in the first example, attempts to use D.Property or D.Method() will result in an exception being thrown, the second will work.

"Hi ,
Now,,Consider D has some reference ..Will it(variable D) be stored in the stack or heap?"


Depends on where it is declared.
If it is in a method: Stack
If it is in a Class: Heap (because the class is a reference type and goes on the heap, so it''s content goes on the heap)
If it is in a Struct: stack (because it is a value type, and they go on the stack)


To add to Griff''s Answer:

The variables can be stored in three places: stack, heap and the process''s static memory area (recommended to keep to minimum). For the reference type, the variable (reference itself) can be stored in all three places, while the memory area pointed by reference is always heap (exclusion is C++/CLI where this limitation is removed due to its unique value semantic for reference types). Structure is a value type, as well as enumeration and primitive types, all other types are reference one.

For simplicity, I will not mention heap below, so just keep in mind the reference is pointing to heap.

Now, it depends on where the variable is declared: if the variable is static, it is stored in static memory area. If the variable is local, it always stored on stack. A field of reference type always goes to stack.

All this rules can be combined, as a structure can have reference-type fields and reference-type static fields, value-type fields and value-type static fields; and a class can have same combination of fields. Figure all possible structures yourself.

—SA


这篇关于创建一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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