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:

True
False

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

[EDIT]

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.

Drop a brain bomb

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s