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.

Drop a brain bomb

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.