I just read this article by MVP Jon Skeet on Parameter passing in C#. Definitely good reading. It helps to understand exactly how reference and value types work when passing them through methods. Here is a good example of how this helped me:
- Instantiate an instance of Class1 as obj1.
- Instantiate Class2 as obj2.
- Pass obj1 in obj2.Foo(obj1)
- The obj2 also has an instance var for Class1 called myobj1.
- In obj2.Foo(Class1 yourobj1), assign myobj1 = yourobj1.
My question has been if myobj1 is a clone of the value of obj1, or if it’s a real reference to the same value. Indeed both references point to the same space, so I need to be careful about how the instance is handled as I pass it around an application. In VB the word SET would have made this all obvious. The article also made it very clear how passing a reference type with a ref qualifier worked – something that can be very confusing.
See Jon’s other articles and his blog for other good info.
Awesome! Gotta get me one tomorrow! http://t.co/xbpGrGNF INTRODUCING THE WORLD'S FIRST HOME MINI-NUCLEAR GENERATOR from @TigerDirect
Improve your OO coding with refactoring. Just take an hour or so to click through these tips: http://t.co/AghAGEbT
Fascinating blog/discussion on (C#) Enumeration classes to replace Enums, and related pros/cons: http://t.co/O2F5vh3M
@dwolt on snupnow: 1) create effective About page: what is it? 2) (advice given to me) get paying clients first, investors follow.
Leave a reply
You must be logged in to post a comment.