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.
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.