Stephen A. Fuqua (saf)

a Bahá'í, software engineer, and nature lover in Austin, Texas, USA

Passing Objects Does Not Require 'ref'

I’m willing to admit that I made a mistake, and I share this now for your reference (no pun intended): in C#, your basic data types (int, string, short, bool) are value-types. If you want to pass them to a method and have the original value updated, instead of a copy, then you must pass them with the ref or out keywords. But not so with reference-types (hence the name!), which includes classes.

One gray area: what to do with structs. That last link says: “when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed.” So there you have it. Definitely pays to know the differences between C# and C++!

Posted with : Tech, Microsoft .NET Framework