Friday, January 30, 2015

C# Reference Value

I often run into situations in C# where my variables don't behave as I expect them to.  For example, I pass an instance of a custom object into another method and the attributes of that instance are changed even though I didn't specify the ref keyword.  That's because C# has value and reference types and which one you get depends on what you're declaring.  This also affects where the memory is stored (the two concepts are tied together to a degree).

In short, when you're using an instance of a custom object, your resulting variable will be a reference type.  Every time.  I know this article is a bit outdated, but it explained it very clearly to me (for the first time) so I'm passing it along (and remember, this blog is all about keeping track of my own answers).

No comments:

Post a Comment