Memory Usage
-
Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:
public class ClassA { // other junk } public class ClassB { // other junk public string GetClassName(ClassA classA) { classA.OtherMethodClass(); return classA.ToString(); } } public class ClassC { // other junk ClassA a = new ClassA(); // set a's properties ClassB b = new ClassB(); string x = b.GetClassName(a); Console.WriteLine(x); }
Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.
-
Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:
public class ClassA { // other junk } public class ClassB { // other junk public string GetClassName(ClassA classA) { classA.OtherMethodClass(); return classA.ToString(); } } public class ClassC { // other junk ClassA a = new ClassA(); // set a's properties ClassB b = new ClassB(); string x = b.GetClassName(a); Console.WriteLine(x); }
Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.
-
Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:
public class ClassA { // other junk } public class ClassB { // other junk public string GetClassName(ClassA classA) { classA.OtherMethodClass(); return classA.ToString(); } } public class ClassC { // other junk ClassA a = new ClassA(); // set a's properties ClassB b = new ClassB(); string x = b.GetClassName(a); Console.WriteLine(x); }
Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.
And if you pass a ValueType like int, DateTime, ... then a copy is passed to the method, as long as you do not mark them with ref or out. The copy is then pushed to the Stack where the method gets it from.
-^-^-^-^-^- no risk no funk ................... please vote ------>
-
You only pass a reference to the object. Objects are never copied in .NET unless you specifically copy them.
--- single minded; short sighted; long gone;
So if I make any changes to the object passed through the method parameter, those changes will also effect the actual object?
public class ClassA { public ClassA() {} public string Color = "orange"; // other junk } public class ClassB { public ClassB(){} // other junk public string GetClassName (ClassA classA) { classA.Color = "red"; classA.OtherMethodClass(); return classA.ToString(); } } public class ClassC { public ClassC() { // other junk ClassA a = new ClassA(); // set a's properties Console.WriteLine(a.Color.ToString()); // Will write orange ClassB b = new ClassB(); string x = b.GetClassName(a); Console.WriteLine(a.Color.ToString()); // Will this write Red? a.Color = "Green"; } }
-
So if I make any changes to the object passed through the method parameter, those changes will also effect the actual object?
public class ClassA { public ClassA() {} public string Color = "orange"; // other junk } public class ClassB { public ClassB(){} // other junk public string GetClassName (ClassA classA) { classA.Color = "red"; classA.OtherMethodClass(); return classA.ToString(); } } public class ClassC { public ClassC() { // other junk ClassA a = new ClassA(); // set a's properties Console.WriteLine(a.Color.ToString()); // Will write orange ClassB b = new ClassB(); string x = b.GetClassName(a); Console.WriteLine(a.Color.ToString()); // Will this write Red? a.Color = "Green"; } }
Yes it will write "red".
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Ok so I am confused. What happends memory wise when I pass an instance of a class as a method parameter, for e.g.:
public class ClassA { // other junk } public class ClassB { // other junk public string GetClassName(ClassA classA) { classA.OtherMethodClass(); return classA.ToString(); } } public class ClassC { // other junk ClassA a = new ClassA(); // set a's properties ClassB b = new ClassB(); string x = b.GetClassName(a); Console.WriteLine(x); }
Now does a new object get created in memory when I pass the class instance? Or would any changes made to the method parameter would also effect the actual object? Just need some clarification. Thanks for your help.
Guffa answered your question correctly for class instances: passing a class instance to a method will *NOT* copy it; you're passing a pointer to that instance. As Urs mentioned, if you pass a struct instance (say, a System.Int32, a DateTime, or your own custom struct), it *WILL* pass a copy of the struct instance to the method. In summary: class instances are passed by reference (no copying going on), and struct instances are passed by value (copying). As a side note, if you do want to pass values by reference, you can do so using the ref keyword.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Sound The Great Shofar! The apostle Paul, modernly speaking: Epistles of Paul Judah Himango