ints by reference in C#

I wasn’t sure about this but I find this interesting:

object x = 7;
object y = 7;

Console.WriteLine(object.ReferenceEquals(x, x));
Console.WriteLine(object.ReferenceEquals(x, y));

Which prints:


Of course this is because we’re assigning the ints to objects, so they become boxed. This could be very useful to me…


This actually works well except for the case of strings. They are already reference types and apparently they are special cased in .net such that there is always only a single instance of a partictular string.

Author: justinmchase

I'm a Software Developer from Minnesota.