值类型是否包装在分配在堆栈或堆上的引用类型中? [英] Are value types wrapped in reference types allocated on Stack or Heap?

查看:83
本文介绍了值类型是否包装在分配在堆栈或堆上的引用类型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,ClassA是在堆上分配的,但intTest和strTest是在堆上分配的吗?...
请帮忙


In the below example ClassA is allocated on heap but is intTest and strTest allocated on heap?...
Please help


class ClassA // ClassA is allocated on heap
    {

        public int intTest; // Is intTest also allocated on heap??
        public String strTest; // Where is strTest allocated..stack or heap??

        public ClassA()
        {
            intTest = 1;
            strTest = "First";
        }

    }

    class ClassB
    {


        public void fnClassB()
        {
            ClassA A = new ClassA();

            ClassA Aa = new ClassA();

            Console.WriteLine(A.intTest);
            Console.WriteLine(A.strTest);

            fnChangeValue(A,Aa.intTest,Aa.strTest);

            Console.WriteLine(A.intTest); //A.intTest gives the new value since A is allocated on heap.

            Console.WriteLine(A.strTest);

            Console.WriteLine(Aa.intTest); 

            Console.WriteLine(Aa.strTest); //Aa.strTest does not give the value modified in the function, though strTest is a string variable allocated on heap...Why is this...?


        }

        void fnChangeValue(ClassA A, int intTest, String strTest)
        {
            A.intTest = 2;

            A.strTest = "Second";

            intTest = 3;

            strTest = "Third";
        }
    }

Output of the above program is :

1
First
2
Second
1
First

推荐答案

我一直对人们拒绝使用Google感到惊讶.尝试使用Google搜索"C#堆栈和堆".这只是返回的数千次点击之一.

http://csharpcomputing.com/Tutorials/Lesson6.htm [
I''m constantly amazed at people''s refusal to use google. Try googling "c# stack and heap". Here''s just one of the THOUSANDS of hits returned.

http://csharpcomputing.com/Tutorials/Lesson6.htm[^]


本地实例化的值类型进入堆栈.
对象的实例成员在堆上(因为对象在堆上).
如果将值类型装箱,则装箱对象中将包含该值的副本.

至少那是约翰·西蒙斯(John Simmons)有趣的"googl"告诉我的.
Locally instantiated value types go on the stack.
Instance members of an object are on the heap (since the object is on the heap).
If a value type gets boxed, a copy of the value is contained in the boxing object.

At least that''s what John Simmons funny "googl" thingy told me.


再次阅读我以前的回答.值类型修改无效的事实与它的内存分配方式无关.这是C#编译器设计决定的结果,即传递给方法的值类型将按值"而不是按引用"传递.这意味着将创建值类型的临时副本并将其传递给方法.方法内部所做的任何事情都只会影响COPY,而不会影响原始内容.

为了更改此默认行为,您可以对方法进行编码以期望对值类型的引用,并在调用该方法时显式地按引用传递该方法:

A by ref方法:
Read my preivious answer again. The fact that a value type modification has no effect has NOTHING to do with how it''s memory was allocated. This is a result of the C# compiler design decision that value types passed to a method would be passed "By Value", not "By Reference". This means that a temporary copy of the value type is made and passed to the method. Anything done inside the method can only affect the COPY and not the original.

In order to change this default behavior, you would code the method to expect a reference to a value type, and explicitly pass by reference when calling that method:

A by ref method:
private void DoubleInt(ref int intToDouble)
{ 
     intToDouble *= 2;
}


调用该方法:


call that method:

int myInt = 5;
DoubleInt(ref myInt); //double the value of myInt


这篇关于值类型是否包装在分配在堆栈或堆上的引用类型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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