o**f 发帖数: 76 | 1 我最近编了一些C#程序。有一些关于Memory的问题想请教。
The list class supports dynamic memory allocation. I
can get an element from a list with GetValue(). However,
if I modify the element I got via GetValue(), the real
element in the list is changed too. It seems that
GetValue returns the reference to that element (The C++
reference concept). Can anyone briefly describe how
memory space is managed by C#? For example, how C#
manage the memory space used by a list.
Thank you! | p*p 发帖数: 75 | 2 reference type, value type
box, unbox
garbage collector
this is all you need to know.
by the way, try to add "int" value to the list, get a reference, and modify
it. check the the element in the list is NOT changed. :)
【在 o**f 的大作中提到】 : 我最近编了一些C#程序。有一些关于Memory的问题想请教。 : The list class supports dynamic memory allocation. I : can get an element from a list with GetValue(). However, : if I modify the element I got via GetValue(), the real : element in the list is changed too. It seems that : GetValue returns the reference to that element (The C++ : reference concept). Can anyone briefly describe how : memory space is managed by C#? For example, how C# : manage the memory space used by a list. : Thank you!
| o**f 发帖数: 76 | 3 Thank you for your reply.
How to distinguish reference type and value type?
I know keywords like "ref" or "out" can take variables
back from a function call, but the example I mentioned
regarding List does not use these keywords.
I assume if I don't use any pointer like variable to
apply memory space, I don't need to do garbage collection.
Is this correct?
I don't know box and unbox. Can you tell me the meaning
of them or I can go to check them out online.
Thanks.
【在 p*p 的大作中提到】 : reference type, value type : box, unbox : garbage collector : this is all you need to know. : by the way, try to add "int" value to the list, get a reference, and modify : it. check the the element in the list is NOT changed. :)
| p*p 发帖数: 75 | 4 this is about allocation of objects. Objects can be allocated in heap, or on
stacks. if you are familar with C/C++, this concept is quite easy to know. if
you create a object by malloc() or new(),
int* p = malloc(sizeof(int))
the object is on the heap, otherwise, if you define a object in a function,
like
int n
it's on stack.
in C/C++, you have total control on where the objects are allocated. but in
C#, it depends on whether it's a reference type or value type. all primitive
types and s
【在 o**f 的大作中提到】 : Thank you for your reply. : How to distinguish reference type and value type? : I know keywords like "ref" or "out" can take variables : back from a function call, but the example I mentioned : regarding List does not use these keywords. : I assume if I don't use any pointer like variable to : apply memory space, I don't need to do garbage collection. : Is this correct? : I don't know box and unbox. Can you tell me the meaning : of them or I can go to check them out online.
| o**f 发帖数: 76 | 5 Hey Pip,
Really appreciate your explainations and these are very helpful for me.
Thank you!
OSPF
if
code
the
in
【在 p*p 的大作中提到】 : this is about allocation of objects. Objects can be allocated in heap, or on : stacks. if you are familar with C/C++, this concept is quite easy to know. if : you create a object by malloc() or new(), : int* p = malloc(sizeof(int)) : the object is on the heap, otherwise, if you define a object in a function, : like : int n : it's on stack. : in C/C++, you have total control on where the objects are allocated. but in : C#, it depends on whether it's a reference type or value type. all primitive
|
|