Pass by value
-
"One notable quirk occurs when you use the standard pass-by-value mechanism. In this e, pass-by-value doesn’t create a copy of the object, but a copy of the reference. This refer- e still points to the same in-memory object. This means that if you pass a Product object t ethod, for example, the method will be able to alter your Product object, regardless of ther you use pass-by-value or pass-by-reference." Beginning ASP.NET 3.5 in C# 2008 (ISBN-13 (pbk): 978-1-59059-891-7) P.69 Can anyone provide a code snippet to demonstrate the above? If the above were a valid statement, wouldn't this be a bug in the .NET 3.5 framework?
Jon
-
"One notable quirk occurs when you use the standard pass-by-value mechanism. In this e, pass-by-value doesn’t create a copy of the object, but a copy of the reference. This refer- e still points to the same in-memory object. This means that if you pass a Product object t ethod, for example, the method will be able to alter your Product object, regardless of ther you use pass-by-value or pass-by-reference." Beginning ASP.NET 3.5 in C# 2008 (ISBN-13 (pbk): 978-1-59059-891-7) P.69 Can anyone provide a code snippet to demonstrate the above? If the above were a valid statement, wouldn't this be a bug in the .NET 3.5 framework?
Jon
-
"One notable quirk occurs when you use the standard pass-by-value mechanism. In this e, pass-by-value doesn’t create a copy of the object, but a copy of the reference. This refer- e still points to the same in-memory object. This means that if you pass a Product object t ethod, for example, the method will be able to alter your Product object, regardless of ther you use pass-by-value or pass-by-reference." Beginning ASP.NET 3.5 in C# 2008 (ISBN-13 (pbk): 978-1-59059-891-7) P.69 Can anyone provide a code snippet to demonstrate the above? If the above were a valid statement, wouldn't this be a bug in the .NET 3.5 framework?
Jon
Pass by value and pass by reference: Passing mechanisms decide how changes made to the parameter affect the caller. Consider the following code snippet Ex: class MyClass { public int value; } class Program { ..void funcPassbyValue(MyClass obj) { obj.value = 10; obj = new MyClass(); obj.value = 5; } ..void func2PassbyRef(ref MyClass obj) { obj.value = 10; obj = new MyClass(); obj.value = 5; } .. Main() { MyClass passedObj = new MyClass(); funcPassbyValue(passedObj ); Console.WriteLine(passedObj .value); //Outputs 10 func2PassbyRef(ref passedObj ); Console.WriteLine(passedObj .value); //Outputs 5 } } You can see both the methods funcPassbyValue && func2PassbyRef differ only in their signature. Things which happen in common to both the methods: Any changes made to the argument to the method will be reflected back (condition is the changes are done while the parameter - in this case obj - is not changed to refer other object). what does funcPassbyValue do differently: any changes made to the parameter (obj) will be local within the method and will not change (change here means to make it refer other object) the passedObj to refer other object. Hope this clears your doublt. what does func2PassbyRefdo differently: any changes made to the parameter (obj) will be reflected back to the caller in this case passedObj is changed to refer to the new object created inside the method.