|
static void Main()
{
Employee e =new Employee();
}
public class Employee
{
static int counter=0;
double mydata = 500;
private string _FirstName;
public string FirstName {
get { return _FirstName; }
set { _FirstName = value ; }
} private string _LastName;
public string LastName {
get { return _LastName; }
set { _LastName = value; }
}
public double CalculateValue() {
double _local=10; mydata=mydata * _local; return mydata ;
}
}
1) i just like to know when i will run my program then who call the static main() function
2) when employee class instance will be created then how memory will be allocated for this class?
3) where this variable will store in heap or stack ?
static int counter=0;
double mydata = 500;
private string _FirstName;
4) where _local variable will be store when calculatevalue will be called ?
5) the important things is where data is stored when we assign data to variable. in stack or heap ?
suppose i have variable called Name="Hello" or Salary=5000 where Hello & 5000 as value will be stored ? heap or stack ?
6) where static variable is stored declared in class?
7) static class load into memory when program just run even if i do not call or use them ?
i heard that memory is allocated when we create instance of non-static class. so in case of static class we can not create instance so how they load into memory. just automatically by CLR when program just invoke or when we use that static class first time ? please explian. thanks
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: 1) i just like to know when i will run my program then who call the static main() function Windows. It loads the assembly, looks for an entrypoint. That your "main" method. To be more specific, the .NET runtime would do that.
Tridip Bhattacharjee wrote: 2) when employee class instance will be created then how memory will be allocated for this class? Some memory will already have been allocated once the assembly is loaded. Once you create an object of that class, it'll need to allocate memory for those fields (not for the methods). Those fields are either valuetypes (full list of their sizes on MSDN[^]) or Reference types (pointers). Unless those point to an object, their memory-allocation will be limited to space for that pointer.
Tridip Bhattacharjee wrote: 3) where this variable will store in heap or stack ?
Being classmembers, heap.
Six important .NET concepts: Stack, heap, value types, reference types, boxing, and unboxing[^]
Tridip Bhattacharjee wrote: 4) where _local variable will be store when calculatevalue will be called ? Stack, as all local declared value-types will be.
Tridip Bhattacharjee wrote: 5) the important things is where data is stored when we assign data to variable. in stack or heap ?
Someone is overstating the importance between stack and heap. I feel a truckload of .ToArray conversions coming up for "performance reasons". Valuetypes to the stack, pointers on the stack pointing to lots of bytes in the heap. That's for the executing method; not for every class loaded in the framework.
Tridip Bhattacharjee wrote: Name="Hello" or Salary=5000 where Hello & 5000
Hello is a string and thus a reference type, salary an integer and a value-type. The first on the heap, second on the stack. Combining them like that would either result in an exception (C#), or a new string (VB) which would be a reference type again.
Tridip Bhattacharjee wrote: 6) where static variable is stored declared in class?
Heap, until it's used.
Tridip Bhattacharjee wrote: 7) static class load into memory when program just run even if i do not call or use them ?
Yes/no. Everything in your assembly will use memory, since it'll be added to the list of "known types loaded". No, it will not run it's static constructor nor allocate memory for it's members until it's first use, as is documented on MSDN.
Tridip Bhattacharjee wrote: i heard that memory is allocated when we create instance of non-static class. so in case of static class we can not create instance so how they load into memory.
By calling it's constructor[^].
Now here are my questions; why did the interface disappear from this example? And why isn't there a reference to a struct? Would an object in a struct be considered a value-type or a reference-type? What about static structs?
Here's some reading;
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thanks for clear answer. i heard that all local variable store on stack. one more question is that if local variable is ref type declared in function and when function will be called then where local variable ref will be stored....in stack or heap. ?
two good question here u highlighted. those are
a) Would an object in a struct be considered a value-type or a reference-type?
b) What about static structs?
c) interface is contract so at all any memory is allocated for interface ?
if possible give answer for that two question.
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: i heard that all local variable store on stack.
You heard wrong. One can declare a string, 1 GB in size, as a local variable. Your stack ain't 1 GB.
Tridip Bhattacharjee wrote: one more question is that if local variable is ref type declared in function and when function will be called then where local variable ref will be stored....in stack or heap. ?
It's a reference. Where would you guess it'd be stored?
Tridip Bhattacharjee wrote: a) Would an object in a struct be considered a value-type or a reference-type?
The struct is a value-type, the object a reference.
Tridip Bhattacharjee wrote: b) What about static structs?
There is no such thing as a static struct. Special case, was a trick question and searching the answer might be fun
Tridip Bhattacharjee wrote: c) interface is contract so at all any memory is allocated for interface ?
Seems I already answered two, this one is yours (and has been answered before )
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
"It just doesn't matter." -- Meat Balls
|
|
|
|