Click here to Skip to main content
15,918,742 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to continuously write text to a label in C# Pin
Member 857809327-Feb-15 9:07
Member 857809327-Feb-15 9:07 
GeneralRe: how to continuously write text to a label in C# Pin
Member 1068390227-Feb-15 11:36
Member 1068390227-Feb-15 11:36 
Questionc# Soap Web service header Pin
Info service26-Feb-15 5:36
Info service26-Feb-15 5:36 
QuestionConfused on method type Pin
Truck5326-Feb-15 3:58
Truck5326-Feb-15 3:58 
AnswerRe: Confused on method type Pin
OriginalGriff26-Feb-15 4:22
mveOriginalGriff26-Feb-15 4:22 
AnswerRe: Confused on method type Pin
manchanx26-Feb-15 5:09
professionalmanchanx26-Feb-15 5:09 
QuestionHow memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 2:56
professionalTridip Bhattacharjee26-Feb-15 2:56 
AnswerRe: How memory is allocated in .Net application PinPopular
OriginalGriff26-Feb-15 3:14
mveOriginalGriff26-Feb-15 3:14 
I'm not going to answer your questions - but not because I'm nasty: because the questions are wrong! Laugh | :laugh:

The distinctions between stack and heap isn't as simple as you might think: and a variable in C# probably isn't what you think it is either - which is at the root of the "wrongness" of your questions.

So: what is a variable?
Simply put, a variable in a method is always allocated on the stack - but it's content may or may not be on the stack, depending on what type of data the variable refers to. If it's a ValueType, then the variable is allocated enough space to hold the whole value: and int is a ValueType, and so is a Point. These are always the same size: you cannot extend the size of a ValueType in any way.
If it's a reference type, then the variable always holds a reference to the actual data, rather than the data itself, and again the variable is a fixed size - only this time it is always either 32 bits or 64 bits depending on the environment your code is executing in. String is a reference type, so is any array.

And that's important: because all reference values are allocated on the Heap: never, ever on the stack.

C#
int i = 6;
Integers are ValueTypes, so "i" is on the stack, and is the value of the integer - in this case 6.
C#
Button b = new Button();
Button is a reference type, so the actual data for the new instance is stored on the heap - but "b" is the variable and it is located on the stack, and contains a reference to the actual instance.

But...that doesn't mean that all ValueType instances are on the stack - they aren't: they can be embedded in reference type instances:
C#
public class MyClass
   {
   public int Value;
   }
...
MyClass mc= new MyClass();
mc.Value = 6;

MyClass is a reference type (all classes are reference types, all structs are value types) so the instance data is on the heap, and that includes the value type integer "Value" it contains. "mc" is on the stack and holds a reference to the heap based actual data.

From here it starts to get very, very complicated - there is something called "boxing" to consider as well, where a stack based value is copied to the heap and passed as a reference - far too complicated for a little text box like this one!

Get a c# book - they all explain this with pictures (which helps a lot) and when you've read it have a look at this: Using struct and class - what's that all about?[^] - it explains a bit more about it, but it's not exactly beginner friendly!

[edit]Typos![/edit]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...


modified 26-Feb-15 10:11am.

GeneralRe: How memory is allocated in .Net application Pin
manchanx26-Feb-15 4:05
professionalmanchanx26-Feb-15 4:05 
GeneralRe: How memory is allocated in .Net application Pin
OriginalGriff26-Feb-15 4:11
mveOriginalGriff26-Feb-15 4:11 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 4:10
professionalTridip Bhattacharjee26-Feb-15 4:10 
GeneralRe: How memory is allocated in .Net application Pin
OriginalGriff26-Feb-15 4:17
mveOriginalGriff26-Feb-15 4:17 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 4:19
professionalTridip Bhattacharjee26-Feb-15 4:19 
GeneralRe: How memory is allocated in .Net application Pin
OriginalGriff26-Feb-15 4:25
mveOriginalGriff26-Feb-15 4:25 
GeneralRe: How memory is allocated in .Net application Pin
jschell26-Feb-15 10:02
jschell26-Feb-15 10:02 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 21:06
professionalTridip Bhattacharjee26-Feb-15 21:06 
GeneralRe: How memory is allocated in .Net application Pin
Pete O'Hanlon27-Feb-15 7:23
mvePete O'Hanlon27-Feb-15 7:23 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee1-Mar-15 21:22
professionalTridip Bhattacharjee1-Mar-15 21:22 
GeneralRe: How memory is allocated in .Net application Pin
manchanx26-Feb-15 4:24
professionalmanchanx26-Feb-15 4:24 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 22:01
professionalTridip Bhattacharjee26-Feb-15 22:01 
GeneralRe: How memory is allocated in .Net application Pin
Pete O'Hanlon26-Feb-15 22:53
mvePete O'Hanlon26-Feb-15 22:53 
GeneralRe: How memory is allocated in .Net application Pin
OriginalGriff26-Feb-15 4:41
mveOriginalGriff26-Feb-15 4:41 
GeneralRe: How memory is allocated in .Net application Pin
Santosh K. Tripathi2-Mar-15 17:07
professionalSantosh K. Tripathi2-Mar-15 17:07 
AnswerRe: How memory is allocated in .Net application Pin
F-ES Sitecore26-Feb-15 3:59
professionalF-ES Sitecore26-Feb-15 3:59 
GeneralRe: How memory is allocated in .Net application Pin
Tridip Bhattacharjee26-Feb-15 4:16
professionalTridip Bhattacharjee26-Feb-15 4:16 

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.