avatar
C#中的Memory控制# DotNet - 窗口里的风景
o*f
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!
avatar
p*p
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!

avatar
o*f
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. :)

avatar
p*p
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.

avatar
o*f
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。