开发者

where does object reside in c#?

开发者 https://www.devze.com 2022-12-16 22:46 出处:网络
i am a java programmer and i know 开发者_如何学Pythonin java objects are stored on heap. Just for curiosity wanted to where does objects reside in c#.

i am a java programmer and i know 开发者_如何学Pythonin java objects are stored on heap. Just for curiosity wanted to where does objects reside in c#.


  • For reference types : on the heap
  • For value types : on the stack for local variables and method parameters, or on the heap for members of a reference type


The C# language doesn't specify where an object or a value should be stored. It simply defines the semantics of reference types and value types.

Microsoft .NET CLR stores values (instances of value types) contained of local variables on stack and instances of reference types (objects) and non-local value types on the heap. However, as stated previously, other implementations of the C# language are free to store things as they wish as long as they conform to value and reference semantics defined by the C# Language Specification.


detailed explaination,

C# Heap(ing) Vs Stack(ing) in .NET: Part I By Matthew Cochran January 14, 2006 http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx


Objects are stored on heap in C# too.


Reference types stored on managed heap.

Value types by default stored on stack.

Value types also can stored on heap in several cases:

  1. During boxing (casting value type to interface, downcasting value type to object etc).
  2. If value type is a member of reference type
  3. If value type uses in closure
0

精彩评论

暂无评论...
验证码 换一张
取 消