Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below example ClassA is allocated on heap but is intTest and strTest allocated on heap?...
Please help


C#
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
Posted

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[^]
 
Share this answer
 
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.
 
Share this answer
 
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
 
Share this answer
 
Object instances are passed by reference, where as other types (int) are passed by value. When you pass in int and string, new scoped local variables are created within fnChangeValue. Any changes to these values do not affect the object (Aa).
 
Share this answer
 
v2
Value types are passed to methods by value. Changing the value of a parameter passed by value has no effect outside the scope of the method. This has nothing to do with how it was allocate, but rather with how it was passed. If you pass a value type by reference
<code >MyMethod(ref myValueType) </code>

then changes to it's value are visible outside the scope of the method.

BTW querying google for "change value type value in method"

brings up as the first hit exactly this information, with a nice example[^]
 
Share this answer
 
A couple of things:

1 - you should press 'answer' if you're posting an answer. That's why it says 'answer'. Use the forum at the bottom, or edit your post to create a 'reply'.

2 - buy a basic C# book and read it. The answer to your question is very simple, although your code is hard to read because you create local variables which hide member variables. C# is behaving exactly as documented, and exactly as made clear in the first few chapters of any half decent C# book. Strings are immutable. Value types are passed by value, not reference. It would not surprise me if the google links provided explain this also. The issue is that you need to do some more basic reading and try to understand the core concepts of how C# works. Perhaps you also need to take a class, if reading it yourself is not helping you.
 
Share this answer
 
John Simmons, You dont seem to understand the question.
FYI, I have been doing googling on this stuff for quite some time and there are no results that answer my question.

My question is very simple:

If i use a value type in a reference type ex: declaring an Int in a class, the value type gets allocated in heap. Given the above, if i change the value of this value type from within a method, its new value does not reflect outside the method? Why is this so?...since the value type declared in a reference type (Class) is allocated on heap, any time the value changes, it should reflect globally?...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900