Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
what is heap value here ? again something to know how its steps in memory ??? i tried alot and at last i get by my knowledge it can be value of x which is basically " Hello World !"

many thanks

C#
class MyClass
{
    String M = " ";

    public MyClass(String message)
    {
        M = value;
    }
}

MyClass X;
X = new MyClass(" Hello World! ");
Posted
Updated 27-Nov-12 20:24pm
v2

1 solution

It's hard to work out what you want to know, but hopefully I have got the gist of your question...

Ignoring that that won't compile, and needs a fair number of changes to do what (I suspect) you intended it to, you have got the wrong idea a bit as well.

If I fix your code a little:
C#
class MyClass
{
    public string M = " ";
 
    public MyClass(string message)
    {
        M = message;
    }
}
 
MyClass X;
X = new MyClass(" Hello World! ");
(Which is no where near perfect, but you'll improve it a bit later - I just want to explain what goes where)
The code creates three items on the heap:
1) A string containing a space
2) A sting containing " Hello World! "
3) A class instance of MyClass

When you call the MyClass constructor by using the new keyword, the parameter string instance is constructed, and then space is allocated for the class instance, finally it's fields are constructed, so space is allocated for a string containing a space. The code of the constructor is then called and the string field M reference is overwritten with the parameter string reference. (This is hard to explain without pictures, but hopefully you will get the idea).


(And yes, purists, strings are special - let it ride for the moment, ok? Let's not confuse the poor soul!)
 
Share this answer
 
Comments
lida zar 28-Nov-12 5:39am    
thanks anyways...

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