简体   繁体   中英

C# managed code reference type question

I have a class that contains some properties, consisting of lists. These lists can be populated with some Sharepoint objects that can be fairly memory intensive.

I pass this class with its list properties through to my function like this:

public void InsertFixedLineItems(CacheBundle cb)
        {
//work here
        }

As you can see the type in question is called a CacheBundle, and at runtime its heavily populated.

For ease of use I want to localize the exact list properties further like this:

public void InsertFixedLineItems(CacheBundle cb)
         {
           List<XYZCacheItem> XYZCacheItems = cb.xyzCacheItems;
           List<YYYCacheItem> YYYCacheItems = cb.YYYCacheItems;
           List<ZZZCacheItem> ZZZCacheItems = cb.ZZZCacheItems; 
         }

My question is, during this assignment above is the code creating a copy of each property, essentially each collection. By doing so waste memory?

Or is the XYZCacheItems merely some kind of pointer to the cb.xyzCacheItems.

If it is not, is it possible to create a "pointer variable" so that if I update XYZCacheItems -> cb.xyzCacheItems gets updated too? At the same time use no extra (or very little) memory and have both assignments.

Lists are reference types, therefore are NEVER copied during assignment or while passing them into a function. XYZCacheItems points to the same object as cb.xyzCacheItems after the assignment, any changes to XYZCacheItems will appear in cb.xyzCacheItems as well.

Variables of reference types, referred to as objects, store references to the actual data.

From MSDN .

You may also want to read some articles about differences between value and reference types (like this one). Understanding this is crucial to work with .Net languages efficiently.

EDIT:

Don't mess up the terminilogy. Pointer is pointer (integer describing specific location in memory), reference is reference (identifier referring to specific managed object, which can be moved around in memory by the runtime, among other things).

You CAN use classic C-like pointers in C#, but they have their drawbacks.

The pointer is copied when you pass it as a parameter ... but the object is not. So the only memory overhead is the allocation of a pointer. Changes the properties of the object pulled from the list will be reflected in the original ... unless you reassign it. If you reassign the pointer in the list the original pointer will still point to the old object.

Parameter passing in c# --> http://msdn.microsoft.com/en-us/library/0f66670z(v=vs.71).aspx

The other answers are good, but I just want to clear up a major assumption we're making, and that is that the property wwwCacheItems does not return a copy of the list. If it isn't, then all is fine.

If any of object references point to the same object, any of these will be modifying the same object.

That's, while you don't create another object, you're creating many references to the same object, which isn't a waste of memory.

Be careful about having heavy objects being referenced in a lot of places, because if some of these are in persistent contexts - like singletons -, these will be never recycled by GAC.

List<T> (really class , interface , and delegate ) are reference types and when you pass them to a method, in fact a pointer of them passed to method. For more information follow this page's sections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM