Click here to Skip to main content
15,879,348 members
Home / Discussions / C#
   

C#

 
GeneralRe: SendMessage Pin
sunsher7-Jul-16 20:28
sunsher7-Jul-16 20:28 
GeneralRe: SendMessage Pin
Richard MacCutchan7-Jul-16 20:39
mveRichard MacCutchan7-Jul-16 20:39 
GeneralRe: SendMessage Pin
sunsher10-Jul-16 12:53
sunsher10-Jul-16 12:53 
GeneralRe: SendMessage Pin
Richard MacCutchan10-Jul-16 22:42
mveRichard MacCutchan10-Jul-16 22:42 
GeneralRe: SendMessage Pin
sunsher11-Jul-16 13:41
sunsher11-Jul-16 13:41 
QuestionStack and Heap Pin
Member 111616257-Jul-16 17:13
Member 111616257-Jul-16 17:13 
AnswerRe: Stack and Heap Pin
Richard MacCutchan7-Jul-16 20:14
mveRichard MacCutchan7-Jul-16 20:14 
AnswerRe: Stack and Heap Pin
OriginalGriff7-Jul-16 20:42
mveOriginalGriff7-Jul-16 20:42 
Not really, no... it's a complicated subject.
The first thing you need to get sorted is the difference between a variable, a reference, and an instance - because without that, none of the rest of this will make any sense.
An instance is an example of a class. Thinking about cars, an instance is a car you can physically get into and drive - it is distinct from all other cars in that it will have a different registration / licence number, may be a different colour, may have a different owner. Each physical car is a different instance of a generic subject "A Car".
A reference is a "pointer" to an instance: "my car" is a reference to a physical vehicle, "your car" is a reference to a different one. "This car" could refer to the same vehicle as "my car" now, and to "your car" in a moment as we physically move from one to another. Clearly, you can have many references to the same instance!
A variable is a named "place" that holds a value - this could be a number like 12 or 14, or a reference to an instance.
So when we say:
C#
Car myCar = new Car("Mercedes", "A160", Color.Red);
We declare a variable called myCar which holds a reference to an instance which is created via the new keyword.
A variable doesn't have to hold a reference: it can hold a value instead:
C#
int i = 666;
We create a variable called i which contains the value 666
This is important, because we have different names for each type of variable: we call the first one a reference type and the second a value type and how they behave is different.

-- We'll get to stack and heap soon, honest! --

When you copy a variable, it's important to know if it's a reference type or value type, because they seem to behave very differently:
C#
int i = 666;
int j = i;
i = i + 1;
Console.WriteLine("{0}:{1}", i, j);
You expect to get "667:666" because if you didn't, then doing any kind of math would get very difficult!
And you do: i and j are value types, so the value in i is copied into j, and then incremented - that doesn't affect the value in j because it's a copy of the value. If you think about variables as "pockets" it makes sense: you have five pennies in your right trouser pocket, and you copy the value to your left trouser pocket - you now have five coins in each pocket. Adding a coin to the right pocket doesn't affect the number of coins in the left (unfortunately).
When you do this with reference types it seems to work very differently:
C#
MyClass a = new MyClass();
a.Value = 666;
MyClass b = a;
a.Value = a.Value + 1;
Console.WriteLine("{0}:{1}", a.Value, b.Value);
This time we get "667:667" because the reference in a is copied to b and so both variables reference the same instance.

-- Stack and heap coming up - very soon now --

It looks like they are doing totally different things, but they aren't, not really - the assignment is doing the same thing in both cases: it's copying the value in one variable to the other variable. It's just that the effects are different because in the first case it copies the value and in the second it copies the reference.

And if any of that make sense, we can now get around to the stack and the heap!

The heap is a big "lump" of memory which is sorted out by the Garbage collector and all classes, methods and threads in your application share it. The stack on the other hand is specific to a thread, and everything on the stack is discarded when you exit the method.
That doesn't mean a lot, does it?

Let's try starting by using them both.
C#
private MyClass MyMethod(MyOtherClass parameter)
   {
   int i = parameter.Index;
   MyClass mc = new MyClass();
   mc.Name = parameter.UserName;
   mc.ID = i + 1;
   return mc;
   }

When you call it, this uses quite a few bits of memory:
1) The stack gets the return address of the method - so it knows where to go back to.
2) The stack gets a copy of the reference to the parameter (the actual class instance is on the heap already)
3) The stack gets an integer called i (which is given a copy of the Index value from the parameter)
4) The stack gets a variable called mc (which can hold a reference to a MyClass instance)
5) The heap constructs an new instance of MyClass and the reference to it is copied to mc
6) The parameter reference is used to identify the instance of MyOtherClass you passed into the method, and it's UserName property is copied to the MyClass instance Name property. This copies a reference to the property and UserName and Name are both reference types.
7) The value in i is fetched, incremented by one, and is copied to the MyClass instance ID property.
8) The reference to the instance of MyClass it returned to the outside world.
9 The method ends, so the stack is reset to the state it was in when the method started - which releases the space occupied by both the i and mc variables - and the return address is fetched from the stack so the method knows where to go back to.

At this point, i and mc no longer exist - they have gone out of scope and the space for them has be recycled. But... the instance of MyClass that mc referred to hasn't - it is on the heap and will only ever be released and recycled by the Garbage Collector. Which means that the mc variable is lost, but the data it references is not - if you have copied the reference to a variable outside the method, then you can still access it from the rest of your program. So the outside world can happily use the reference the method returned:
C#
MyClass newUser = MyMethod(registrationInfo);
Console.WriteLine(newUser.Name);


The heap holds the instances of all reference types, the stack holds all the variables your methods declare.
Does that make sense?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

QuestionStringbuilder printing "System.Collections.Generic.List" instead of the string item in the list, why? Pin
Member 122479807-Jul-16 3:56
Member 122479807-Jul-16 3:56 
AnswerRe: Stringbuilder printing "System.Collections.Generic.List" instead of the string item in the list, why? Pin
Eddy Vluggen7-Jul-16 4:03
professionalEddy Vluggen7-Jul-16 4:03 
AnswerRe: Stringbuilder printing "System.Collections.Generic.List" instead of the string item in the list, why? Pin
Matt T Heffron7-Jul-16 7:35
professionalMatt T Heffron7-Jul-16 7:35 
QuestionHow to convert .docx file to .html with image and equation Pin
Member 44636384-Jul-16 5:32
Member 44636384-Jul-16 5:32 
AnswerRe: How to convert .docx file to .html with image and equation Pin
Eddy Vluggen4-Jul-16 6:27
professionalEddy Vluggen4-Jul-16 6:27 
AnswerRe: How to convert .docx file to .html with image and equation Pin
Kimberly Weldon5-Jul-16 1:13
Kimberly Weldon5-Jul-16 1:13 
AnswerRe: How to convert .docx file to .html with image and equation Pin
koolprasad20036-Jul-16 18:21
professionalkoolprasad20036-Jul-16 18:21 
Questionsimulate Alt+Shift in all objects of project C# Pin
hahaie3-Jul-16 22:22
hahaie3-Jul-16 22:22 
AnswerRe: simulate Alt+Shift in all objects of project C# Pin
OriginalGriff3-Jul-16 22:50
mveOriginalGriff3-Jul-16 22:50 
Questionhow to sum time in grid view footer c# Pin
Member 125037693-Jul-16 20:06
Member 125037693-Jul-16 20:06 
QuestionRe: how to sum time in grid view footer c# Pin
Richard MacCutchan3-Jul-16 22:05
mveRichard MacCutchan3-Jul-16 22:05 
AnswerRe: how to sum time in grid view footer c# Pin
Member 125037693-Jul-16 23:11
Member 125037693-Jul-16 23:11 
GeneralRe: how to sum time in grid view footer c# Pin
Richard MacCutchan3-Jul-16 23:20
mveRichard MacCutchan3-Jul-16 23:20 
QuestionC# - New to programming! Stuck! Can someone help explain this code? Pin
Member 126152642-Jul-16 4:21
Member 126152642-Jul-16 4:21 
AnswerRe: C# - New to programming! Stuck! Can someone help explain this code? Pin
Dave Kreskowiak2-Jul-16 5:15
mveDave Kreskowiak2-Jul-16 5:15 
AnswerRe: C# - New to programming! Stuck! Can someone help explain this code? Pin
Richard MacCutchan2-Jul-16 5:17
mveRichard MacCutchan2-Jul-16 5:17 
AnswerRe: C# - New to programming! Stuck! Can someone help explain this code? Pin
BillWoodruff3-Jul-16 1:55
professionalBillWoodruff3-Jul-16 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.