tag:blogger.com,1999:blog-5430866.post113759083277228841..comments2024-01-02T08:54:14.406-05:00Comments on Peter Bromberg's UnBlog: Value Types, Reference Types, and writing with clarity!peterbromberghttp://www.blogger.com/profile/18173639411723574123noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-5430866.post-1138994830600635232006-02-03T14:27:00.000-05:002006-02-03T14:27:00.000-05:00Bruce,Good point, and yes I have not only ready Jo...Bruce,<BR/>Good point, and yes I have not only ready Jon's piece I actually Teleport-ed hist whole section onto my HD and "FarHTML"-ed it into a searchable CHM, because the whole thing is so well written. <BR/><BR/>Unfortunately, BOTH of the short statements you have corrected (and i do not dispute the corrections) were both lifted - verbatim- from the MS Patterns and Practices whitepaper!peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1138989088750025502006-02-03T12:51:00.000-05:002006-02-03T12:51:00.000-05:00First, I have to agree with anonymous. The stateme...First, I have to agree with anonymous. The statement<BR/><BR/>"If you specify that a reference type should be passed by value, a copy of the reference is made and the reference to the copy is passed."<BR/><BR/>is incorrect. I think that Jon Skeet does an excellent job of explaining this on his page on parameter passing:<BR/><BR/>http://www.yoda.arachsys.com/csharp/parameters.html<BR/><BR/>In particular, he takes pains to note that reference types are not, by default, "passed by reference" but instead references, by default, are passed by value. He goes into detail by what he means by that, but I think that the distinction is worth it.<BR/><BR/>The other statement that isn't correct is this:<BR/><BR/>"Memory for a value type is allocated on the current thread's stack."<BR/><BR/>Yes and no. Memory for a local variable or argument that is a value type is stored on the stack. Memory for a class field that is a value type is stored on the heap, in the memory allocated for the class instance. So, your statement is true for local variables and method arguments, but not for fields in reference types.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1137633633260262512006-01-18T20:20:00.000-05:002006-01-18T20:20:00.000-05:00The results are what I expected. Thanks for the c...The results are what I expected. Thanks for the clear example. This is the statement that was confusing me from the original "unblog".<BR/><BR/>"If you specify that a reference type should be passed by value, a copy of the reference is made and the reference to the copy is passed."<BR/><BR/>I was reading that as: a copy of the reference (as in a deep clone) is made, instead of a copy of the reference variable (the pointer) is made. Anyway, it's crystal clear now. Thanks.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1137628458818964052006-01-18T18:54:00.000-05:002006-01-18T18:54:00.000-05:00Re: "I am really trying". Let's clean that up a bi...Re: "I am really trying". Let's clean that up a bit (results are inline in the code as comments):<BR/><BR/>using System;<BR/>namespace PersonTest<BR/>{<BR/> class Class1<BR/> {<BR/> [STAThread]<BR/> static void Main(string[] args)<BR/> {<BR/> Person p = new Person() ;<BR/> p.FirstName = "George";<BR/> ChangeInstance(p);<BR/> Console.WriteLine("ChangeInstance: "+p.FirstName );<BR/><BR/> Person p2 = new Person() ;<BR/> p2.FirstName = "George";<BR/> ChangeInstance(ref p2);<BR/> Console.WriteLine("ChangeInstance [ref]: "+p2.FirstName );<BR/><BR/> Person p3 = new Person() ;<BR/> p3.FirstName = "George";<BR/> ChangeProperty(p3);<BR/> Console.WriteLine("ChangeProperty: "+p3.FirstName );<BR/><BR/> Person p4 = new Person() ;<BR/> p4.FirstName = "George";<BR/> ChangeProperty(ref p4);<BR/> Console.WriteLine("ChangeProperty: "+p4.FirstName );<BR/><BR/> Console.ReadLine();<BR/><BR/> // Results are:<BR/> //ChangeInstance: George<BR/> //ChangeInstance [ref]: Yoda<BR/> //ChangeProperty: John Change property<BR/> //ChangeProperty: John Change property (ref)<BR/><BR/>}<BR/><BR/>static void ChangeInstance(Person p)<BR/>{<BR/>p = new Person();<BR/>}<BR/><BR/>static void ChangeInstance(ref Person p)<BR/>{<BR/>p = new Person();<BR/>p.FirstName ="Yoda";<BR/>}<BR/><BR/>static void ChangeProperty(Person p)<BR/>{<BR/>p.FirstName = "John Change property";<BR/>}<BR/><BR/>static void ChangeProperty(ref Person p)<BR/>{<BR/>p.FirstName = "John Change property (ref)";<BR/>}<BR/>}<BR/><BR/> class Person<BR/> {<BR/> public string FirstName;<BR/> }<BR/>}peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1137618064507974572006-01-18T16:01:00.000-05:002006-01-18T16:01:00.000-05:00void ChangeProperty(ref on p)should be void Change...void ChangeProperty(ref on p)<BR/><BR/>should be <BR/><BR/>void ChangeProperty(ref Person p)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1137614455996428782006-01-18T15:00:00.000-05:002006-01-18T15:00:00.000-05:00I am really trying to get a handle on this subject...I am really trying to get a handle on this subject and everything I read seems to be different. Based on my testing this statement is not true: "If you specify that a reference type should be passed by value, a copy of the reference is made and the reference to the copy is passed."<BR/><BR/>Passing by either byvalue or byref allows the original object to be changed in the called method. The difference is:<BR/><BR/>void ChangeInstance(Person p)<BR/>{<BR/> p = New Person<BR/>}<BR/><BR/>does not change the original instance of p, whereas,<BR/><BR/>void ChangeInstance(ref Person p)<BR/>{<BR/> p = New Person<BR/>}<BR/><BR/>changes the original instance of p to the new instance.<BR/><BR/>These methods,<BR/><BR/>void ChangeProperty(Person p)<BR/>{<BR/> p.FirstName = "John"<BR/>}<BR/><BR/>void ChangeProperty(ref on p)<BR/>{<BR/> p.FirstName = "John"<BR/>}<BR/><BR/>produce the same result.<BR/><BR/>It is the same for both vb and c#. At least that's the way I understand it.<BR/><BR/>In addition, I see no need to pass a reference type byref. If I am going to change the instance, I prefer to use a function and return the new object rather than change an argument.<BR/><BR/>Your comments explaining where I have this wrong would be greatly appreciated.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1137602132416580672006-01-18T11:35:00.000-05:002006-01-18T11:35:00.000-05:00The post was specifically targeted at the C# langu...The post was specifically targeted at the C# language since the book I was "complaining" about was specifically about C#.<BR/><BR/>Besides some semantic differences in VB.NET, such as being able to force parameters to be passed by value, regardless of how they are declared by enclosing the parameters in extra parentheses, I'm not aware of any differences. But then, VB.NET has so much baggage inherited from years back that it might be hard to tell!peterbromberghttps://www.blogger.com/profile/18173639411723574123noreply@blogger.comtag:blogger.com,1999:blog-5430866.post-1137598228409641862006-01-18T10:30:00.000-05:002006-01-18T10:30:00.000-05:00I agree with your comments on value types. I quest...I agree with your comments on value types. I question your comments on the reference type assuming I read the comment correctly. This may be due to a difference in C# and VB. Using by value (ByVal in VB) with a reference type, passes the pointer to the reference type on the heap, and using by Reference (ByRef) passes a pointer to the pointer for the reference type on the heap. In either case, changes to the object can be seen by the client code. The difference between the two methods of passing a reference type: with byval, if the argument is changed to a different instance of the class, the client code does not see the change; with byref, the client code sees the different instance of the class, and the original instance's pointer is lost. IMO, for reference types, the only difference between byval and byref is if the called method changes the instance of the class passed.<BR/><BR/>Please correct me if I am wrong, or misunderstood your comment.Anonymousnoreply@blogger.com