tag:blogger.com,1999:blog-6393051114813114443.post108296854282539098..comments2024-03-27T14:52:51.318-05:00Comments on Lee's Blog: Parameter passing in C#Lee Richardsonhttp://www.blogger.com/profile/01314803491511307042noreply@blogger.comBlogger24125tag:blogger.com,1999:blog-6393051114813114443.post-32338381020814011842014-06-10T13:38:36.698-05:002014-06-10T13:38:36.698-05:00Great article ! Very enlightening - thank you for...Great article ! Very enlightening - thank you for this.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-86626374609992794632013-06-28T05:35:22.445-05:002013-06-28T05:35:22.445-05:00This comment has been removed by a blog administrator.Anonymoushttps://www.blogger.com/profile/14178155909391788234noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-83779380038238636182013-05-19T15:23:24.742-05:002013-05-19T15:23:24.742-05:00I don't see what you mean by dereferenced beca...I don't see what you mean by dereferenced because this would mean dis-relating things that were previously related.<br /><br />Then all references to the inner variable are implicitly dereferenced for you ...<br /><br />well, if changes to j are reflected in i, then they can't be DE referenced. <br /><br />What did I miss?Anonymoushttps://www.blogger.com/profile/00272393807852233007noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-32129258854003850802013-05-19T15:20:58.174-05:002013-05-19T15:20:58.174-05:00Lee, I'm confused about a semantics point (in ...Lee, I'm confused about a semantics point (in the English sense)<br /><br />Passing by reference doesn't involve an <b> implicity </b> should be implicit :) copy, instead it instantiates the inner variable to the address in memory of the outer variable. <b> Then all references to the inner variable are <i> implicitly </i> dereferenced for you </b> and voila, magically you're changing the value of the outer variable.<br /><br />I don't get what "dereferenced" means:<br /><br />Then all references to the inner variable are implicitly dereferenced for you ...<br /><br />It would seem my understanding of what is happening is correct if the terms val and ref are reversed :)<br /><br />If something is DE referenced, then there is NO connection between the 2 items. So changes in j can't affect i but they do.<br /><br />The change in i in diagram 6 could not happen if the reference location of i and j were gone ... or what did I miss?<br /><br />j would HAVE to be referring (referencing) the same address because changes to j affect i.<br /><br />Please clarifyAnonymoushttps://www.blogger.com/profile/00272393807852233007noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-24154815537591171102012-02-13T19:35:24.956-05:002012-02-13T19:35:24.956-05:00Anonymous above^:
#5 and #7 are not the same. Yo...Anonymous above^:<br /><br />#5 and #7 are not the same. You must read the rest of the article.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-28938963384049783132010-05-09T21:21:52.335-05:002010-05-09T21:21:52.335-05:00Thanks Riachardson, very useful article. one quick...Thanks Riachardson, very useful article. one quick question when one would use between #5 and #7 ? since end result is same ,is there an advantage one over the other ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-84069336707730420492008-12-12T13:16:00.000-05:002008-12-12T13:16:00.000-05:00One thing that is missing in your discussion and J...One thing that is missing in your discussion and Jon's is the effect of new() on reference parameters.<BR/><BR/>I know it has unique behaviors - I just can't remember the details - which is why I came to your article and Jon's on this topic - so I'll have to keep looking. :)<BR/><BR/>Thanks for an otherwise very good article.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-69961296404304615892008-08-15T04:00:00.000-05:002008-08-15T04:00:00.000-05:00Pictures do help immensely with understanding valu...Pictures do help immensely with understanding value objects and reference objects, thanks for the great article.<BR/><BR/>I was playing around with Visual C++ recently, and noticed something interesting. Reference objects in C# are managed pointers (or whatever they're called) in C++. You can tell because they have ^ after them. Suddenly, passing reference objects by value makes sense if you think about it from C++.<BR/><BR/>Here's what I think is going on. Let's say a reference object is a pointer to the object in memory. You pass this pointer by value. C# automatically dereferences the pointer for you when you work with it. someString.replace() might be something like someString->replace(). You know that if you assign this pointer to something else (another object, null, new object), it doesn't affect the original pointer. So you can modify the contents of the reference object because of automatic dereferencing, you just can't change what it points to.<BR/><BR/>So, basically, reference objects are pointers and dereferencing is automatic. Did I get it right?Unknownhttps://www.blogger.com/profile/06647572980867348635noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-53186446652890681422008-03-31T23:45:00.000-05:002008-03-31T23:45:00.000-05:00Hi Mr. Richardson, I really enjoyed reading your...Hi Mr. Richardson,<BR/> I really enjoyed reading your article. I always love to learn by drawing pictures - for every math problem i do i draw pictures and make it very visual. i am a C++ programmer and i have always drawn pictures like this to understand pointers and passing parameters. So i really really appreciated what you have done for people like us. one picture is equivalent to 1000 words! thanks again for your wonderful article.<BR/><BR/> I have a question. this is in combination with #3 and #5. What happens when the immutable string is passed by value? <BR/><BR/>string s1 = "hello";<BR/>Change(s1);<BR/>Console.Writeln(s1.ToString());<BR/><BR/>void Change(string s2)<BR/>{<BR/> s2 += " world";<BR/>}<BR/><BR/>From what i understand from all your explanations, when Change method is called, s2 is allocated on the stack and gets the address of "hello" (will have the same value as s1 - i.e s2 will be 'pointing' to "hello". but when the statement s2 += " world"; is executed, a new string "hello world" is allocated on the heap and s2 now points to that. When we leave the Change method, s2 is deallocated on the stack. Is this what really happens? am i understanding it correctly? <BR/>So will the Writeln put out the answer hello? <BR/>Am i correct in understanding it this way?<BR/><BR/>Regards,<BR/>KalaUnknownhttps://www.blogger.com/profile/07913421511172326053noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-28409857164925056912008-02-22T09:03:00.000-05:002008-02-22T09:03:00.000-05:00Sergy,Yup, strings are definitely reference types,...Sergy,<BR/><BR/>Yup, strings are definitely reference types, just immutable ones. So it sounds like there might be some duplication, but there really isn't because of string interning. Check out my post to L Kujonewicz (8 posts down) which references the following article: http://www.codeproject.com/books/0735616485.asp.Lee Richardsonhttps://www.blogger.com/profile/01314803491511307042noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-85674576030605343482008-02-22T07:06:00.000-05:002008-02-22T07:06:00.000-05:00Thank you for a good article. It was a question fo...Thank you for a good article. It was a question for me if string parameter is passed into function as a value type (what would be very non-productive). Now I quess string is duplicated within function only if it is changed there.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-75660547387163595872008-01-28T04:14:00.000-05:002008-01-28T04:14:00.000-05:00Hi,Its a great article that you have. It explains ...Hi,<BR/>Its a great article that you have. It explains the concept step-by-step, reaching in the end the most difficult part (Reference Types Passed by Reference), which I was actually looking for. <BR/><BR/>Shall bookmark the page right away..for future reference! :-D<BR/><BR/>Am highly thankful to Jon & U for your efforts. Thanks.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-86578891455511310112007-12-11T04:38:00.000-05:002007-12-11T04:38:00.000-05:00First of all, THANKS for providing such a useful a...First of all, THANKS for providing such a useful and beautifully designed post. With this I have got all my confusions regarding "value" and "ref" cleared.<BR/><BR/>However it took me some time to understand #6 [Value Types Passed by Reference] and #7 [Reference Types Passed by Reference]. Because in existing pictures j [in #6] seems like a NEW ref variable pointing to i. So as sb2 [in #7] seems like a NEW ref variable pointing to sb1 and that confuses how <BR/><I>sb2.Append(" world");</I> <BR/>a ref [sb2] to ref [sb1] changes value of an object pointed by first ref [sb1].<BR/><BR/>Since there is no new memory is allocated when passing by "ref" there should not be a seprate box for j [in #6] and sb2 [in #7]; instead the BOX for i and sb1 should be shown as pointed by j and sb2 respectively. For difference, i and sb1 should be in light [gray] color while j and sb2 should be in dark [black] color stating that the same location is accessible in function using these darker variables.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-52636891514179067512007-10-09T06:50:00.000-05:002007-10-09T06:50:00.000-05:00Wow, I can't believe no one spotted that before no...Wow, I can't believe no one spotted that before now. Nice job Daniel, you're absolutely correct. I'll update that shortly.Lee Richardsonhttps://www.blogger.com/profile/01314803491511307042noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-81158569600913401602007-10-08T12:42:00.000-05:002007-10-08T12:42:00.000-05:00Dear Mr. Richardson.I'm currently learning the bas...Dear Mr. Richardson.<BR/>I'm currently learning the basics of C# and I must say that your article has clarified many issues that has confused me in the past regarding reference types, and the pictures certainly helped in the process. So thank you so much for this article!<BR/><BR/>Regarding #6, I believe that the code in the image is missing a line of code. After the declaration of the method Change, there are two operations, and there's no code for the second one. I believe that the line missing is "j = 10;".<BR/>Please check that segment of the article and change it (or respond and tell me why I'm mistaken).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-37227813917278056722007-04-24T07:29:00.000-05:002007-04-24T07:29:00.000-05:00By the way, if you liked this article you may also...By the way, if you liked this article you may also like my <A HREF="http://rapidapplicationdevelopment.blogspot.com/2007/02/boxing-and-unboxing.html" REL="nofollow">Boxing and Unboxing</A> article.Lee Richardsonhttps://www.blogger.com/profile/01314803491511307042noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-2516967570116874542007-04-17T11:20:00.000-05:002007-04-17T11:20:00.000-05:00L Kujonewicz,Regarding your first post, I had to r...L Kujonewicz,<BR/><BR/>Regarding your first post, I had to research your point to double check whether:<BR/><BR/>string s1 = "hello";<BR/>string s2 = s1;<BR/><BR/>Results in one "hello" on the heap or two. It turns out that if you compare the two addresses via:<BR/><BR/>Console.WriteLine((object)s1 == (object)s2);<BR/><BR/>The result is actually true (you have cast to avoid the string operator overloading). So there is only one "hello" on the heap.<BR/><BR/>It turns out, however, that even:<BR/><BR/>string s1 = "hello";<BR/>string s2 = "hello";<BR/><BR/>Will return true if you compare the two addresses, because of String Interning. Here's a great article on the details: <BR/> http://www.codeproject.com/books/0735616485.aspLee Richardsonhttps://www.blogger.com/profile/01314803491511307042noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-66369438396322189182007-04-17T09:44:00.000-05:002007-04-17T09:44:00.000-05:00zytan,You're second post is absolutely correct. I...zytan,<BR/><BR/>You're second post is absolutely correct. I changed the text to read "there won't be when passing by reference." Thanks for pointing it out.Lee Richardsonhttps://www.blogger.com/profile/01314803491511307042noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-4326892396556967832007-03-10T14:25:00.000-05:002007-03-10T14:25:00.000-05:00And actually, I'm a bit confused about the last st...And actually, I'm a bit confused about the last step in #7. There is a lot of implicit de-referencing going on, so I want to be clear.<BR/><BR/>When you set sb2 = null, what does that really do? Does it set the thing that sb2 points to, to null? Or does it set sb2 itself to null (and is there a difference?)<BR/><BR/>At the very end, does sb2 still point to sb1 (which now contains a null pointer?). Or does it point to null itself?<BR/><BR/>I'm guessing that because these reference types were passed BY REFERENCE, that whenever you mess with them, everything is automatically de-referenced all the way up the chain. So perhaps the final diagram should show both sb1 and sb2 containing null in their boxes?Unknownhttps://www.blogger.com/profile/11087674520213010910noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-73968652299373543352007-03-10T14:20:00.000-05:002007-03-10T14:20:00.000-05:00I think your diagram in #3 is incorrect, from what...I think your diagram in #3 is incorrect, from what I understand. An immutable reference type such as a string works differently, as you say. But your diagram doesn't explain why/how s2.append ends up not affecting the string that s1 points to.<BR/><BR/>I think in the second step, rather than showing s2 and s1 both pointing to the same string, you should show that there is an implicit creation of a new string, and s2 is given a reference to it. That way, when you mess around with s2, you are affecting another instance of a string. The #1 and #3 steps of your diagram still make sense, though.<BR/><BR/>Please correct me if I'm wrong.Unknownhttps://www.blogger.com/profile/11087674520213010910noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-2060420170211700232007-02-26T18:32:00.000-05:002007-02-26T18:32:00.000-05:00Ah, wait, the text in #5 "And did you notice there...Ah, wait, the text in #5 "And did you notice there's an implicit assignment statement when passing by value? As you'll see shortly there won't be for reference types" is referring to #5 itself. "did" means in the past, so I thought you meant in #4, that should be fixed. And "you'll see shortly" doesn't mean the image coming up in #5, it means in #7 (Reference Types Passed by Reference). That's ambiguious. And "reference types" should be "Reference Types Passed by Reference" since both #5 and #7 are about reference types. Ok, was that correct?Unknownhttps://www.blogger.com/profile/10345516125178674013noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-32023005779579364992007-02-26T18:28:00.000-05:002007-02-26T18:28:00.000-05:00You just taught me that strings are immutable refe...You just taught me that strings are immutable references, cool. Ok, but for point #5 you said "did you notice there's an implicit assignment statement when passing by value? As you'll see shortly there won't be for reference types". I assumed the implicit assingment was the blue arrow, which I see for both value and reference passing. So, I don't follow this part of the article. Great pictures, though, they help a lot!Unknownhttps://www.blogger.com/profile/10345516125178674013noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-83047631874667214572007-02-09T05:56:00.000-05:002007-02-09T05:56:00.000-05:00Good catch nandu. Got the diagram right, but the ...Good catch nandu. Got the diagram right, but the text wrong. I've fixed it now.Lee Richardsonhttps://www.blogger.com/profile/01314803491511307042noreply@blogger.comtag:blogger.com,1999:blog-6393051114813114443.post-19218210159567235142007-02-08T14:02:00.000-05:002007-02-08T14:02:00.000-05:00The Answer for Quiz in section 3 ("3. Immutable Re...The Answer for Quiz in section 3 ("3. Immutable Reference Types") should be just "Hello" NOT "Hello World".Nanduhttps://www.blogger.com/profile/06115151118324590791noreply@blogger.com