0

little query re: how .net works with system memory..

i pass a list object to a method:

DoMyThing(myListObj);

public void DoMyThing(List<string> myListObj)
{
    var a = myListObj;
}

in this scenario, is a a 'copy' of, or a pointer to the myListObj?

to make this slightly different... here a dictionary say (assume of single item), and 'pick' something out of that object, does that copy data, or point to it?

DoMyThing(myDictObj);

public void DoMyThing(Dictionary<string, string> myDictObj)
{
    var b = myDictObj.First().Value;
}

ultimate goal here is trying to make some of my code a bit more memory optimal.. there are situations where an object i send to a method can be ~100mb

thanks!

m1nkeh
  • 1,337
  • 23
  • 45
  • `a` is pointing to the same object as `myListObj`. You can test this for yourself by `Add`ing an entry to it and seeing that the other object has the same new entry. – mjwills Jan 04 '19 at 11:24
  • In your samples, you are passing these values by reference. You should read about the data types and if they are value types (int, boolean, etc) or not. – Svek Jan 04 '19 at 11:24
  • 1
    `and 'pick' something out of that object, does that copy data, or point to it?` It will be pointing to the same string. If it was a value type, it would copy it instead. – mjwills Jan 04 '19 at 11:24
  • Also, the way to optimize memory usage is to use a profiler. Not take wild stabs at things that "might" be using memory. – Damien_The_Unbeliever Jan 04 '19 at 11:41
  • Careful about what "pointer" means to you though. `a` cannot be dereferenced; if you assign a new value to it the entry in `myListObj` doesn't change. If you want to modify an object assigned to `a` then reference vs. value type comes into play. – IceGlasses Jan 04 '19 at 13:34

0 Answers0