内存分配对堆和栈的C#普通班静态类静态方法instanve方法和数据成员? [英] Memory allocation for regular class static class static method instanve method and data member on heap and stack c#?

查看:272
本文介绍了内存分配对堆和栈的C#普通班静态类静态方法instanve方法和数据成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大部分的书从不谈论程序是如何执行和内存是如何分配他们在栈和堆。当存储栈和堆时的数据。

Most of the book never talk about how a program execute and how memory is allocated for them on stack and heap. when data stored on stack and when on heap.

假设我有一个普通的类如

Suppose i have one regular class like

public class MyClass
{
    int Age=0;
    string strName="None"
    Double Salary=0;

    public void Data1()
    {
        Age=30;
        strName="Robin";
        Salary=3500;
    }
}



问题



1),因此,对于上面级内存如何分配。程序运行时内存将被分配或当我们创建实例,那么内存将被分配。当我们调用数据1()通过实例,然后发生了什么和内存将如何分配。内存将被分配给函数调用或数据成员初始化?告诉我多少内存将为年龄被分配名称薪水。内存将在堆栈或堆上分配。

Question

1) so for the above class how memory will be allocated. when program run the memory will be allocated or when we create instance then memory will be allocated. when we call Data1() through instance then what happen and how memory will be allocated. memory will be allocated for function call or for data member initialization? tell me how much memory will be allocated for age, name and salary. memory will be allocated on stack or heap.

public class MyClass
{
    static int y=0; 
    static string strComp="None"
    int Age=0;
    string strName="None"
    Double Salary=0;

    public void Data1()
    {
        Age=30;
        strName="Robin";
        Salary=3500;
    }

    public static void Data3()
    {
         y=50;
         strComp="Hello";
    }
}



2)如何以及何时分配内存为静态数据件和功能。当我们调用像
MyClass.Data3()那么内存将被分配或当我们刚刚运行的应用程序。内存分配在堆或栈?

2) how and when memory is allocated for static data member and function. when we call like MyClass.Data3() then memory will be allocated or when we just run the apps. memory is allocated on heap or stack?

3)memroy的分配方式静态类。静态类存储在堆或栈...如果叠加,那么为什么?

3) how memroy is allocated for static class. static class stored on heap or stack...if stack then why?

在这里,我问问几个问题,请详细介绍一下。谢谢。

here i asked asked couple of question please explain in detail. thanks.

推荐答案

在创建类的实例1)内存将被分配。当你调用数据1()不需要额外的内存,你只引用类实例(也没有其他的局部变量)的领域。由于 MyClass的是引用类型,内存将在托管堆中分配的。

1.) Memory will be allocated when you create an instance of the class. When you call Data1() no additional memory is needed as you are only referencing fields of the class instance (and no other local variables). Since MyClass is a reference type, memory will be allocated on the managed heap.

2)静态方法做不消耗任何内存。您可以访问任何静态字段或创建它们(在这种情况下, MyClass的)中包含的类型的任何实例

2.) Static methods do not consume any memory. Static fields are initialized before you access any static field or create any instance of the type they are contained in (MyClass in this case)

3。)你不能创建所以没有内存是动态分配的,只有当类型本身创建一个静态类的一个实例。静态类保证一定装载,并有他们的田地初始化,类是在程序中首次引用之前调用其静态构造函数。一旦创建一个静态类保留在内存中,直到你的应用领域将被关闭。

3.) You cannot create an instance of a static class so no memory is allocated dynamically, only when the type itself is created. Static classes are guaranteed to be loaded and to have their fields initialized and their static constructor called before the class is referenced for the first time in your program. Once created a static class remains in memory until your application domain is shut down.

这篇关于内存分配对堆和栈的C#普通班静态类静态方法instanve方法和数据成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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