rss resume / curriculum vitae linkedin linkedin gitlab github twitter mastodon instagram
Comparing Ruby and C#: Equality
Mar 17, 2010

Beauty

While reading The Ruby Programming Language I wrote a couple of notes about the language comparing it to C#. This is the first post of the series talking about those notes.

C# and Ruby share a similar syntax to compare equality in objects. Both use the operator equals (==) and, at least, one method to compare. Ruby uses equal? and eql?, C# uses Equals. Also, both support overriding the equals (==) operator to provide a different logic in case that's required. The methods' name are different but they work pretty much the same.

Understanding the difference between both languages is really simple. If you already know the difference between reference types and values types you are pretty much all set.

Ruby

Method equal?

Method used to test reference equality in two objects. For example:

#!/usr/bin/env ruby

a = 0
b = 0.0
c = b
d = e = 0

# "false" pointer c points to b, and b and a 
# are different types.w
puts "c.equal?(a) #{c.equal?(a)}" 
# "false" b and a are different types
puts "b.equal?(a) #{b.equal?(a)}" 
# "true" Same type, same value. 
puts "d.equal?(e) #{d.equal?(e)}"</pre>

Method eql?

Synonym of equal?, not strict type conversion. Notice Hash classes uses this method for creating the hash, so if two values are the same the hash method should return the same value.

#!/usr/bin/env ruby

a = 0
b = 0.0
c = b
d = e = 0

# "false" Pointer c points to b, and b and a are different types
puts "c.eql?(a) #{c.eql?(a)}" 
# "false" Different types
puts "b.eql?(a) #{b.eql?(a)}" 
# "true" Same type, same value. 
puts "d.eql?(e) #{d.eql?(e)}"

Operator equals (==)

By default, in Object class, it's a synonym of equal?. Tests reference equality.

#!/usr/bin/env ruby

a = 0
b = 0.0
c = b
d = e = 0

# "true" Even when pointer c points to b, and b and a 
# are different types, the value is the same
puts "c == a #{c == a}" 
# "true" Type is casted to allow comparing them
puts "b == a #{b == a}" 
# "true" Same type, same value. 
puts "d == e #{d == e}"

C#

Before explaining the equality options, notice one important difference between Ruby and C#.

First, Ruby is a dynamic typed language. When declaring variables there’s no meaning of variable type, all variables can be used to identify instances of different types depending on the situation. For example, we can define a variable x to act as a string, and then use the same variable x to act as an integer, this doesn’t mean we are converting the string to integer, this means we are using the same pointer (variable x) for two different types, string and integer, pointing to two different addresses in memory. For example:

#!/usr/bin/env ruby

a = "I'm string"
# Output: "a Value: 'I'm string' Class: 'String'"
puts "a Value: '#{a}' Class: '#{a.class}'"

# Output: "a Value: '10.0' Class: 'Float'"
a = 10.0
puts "a Value: '#{a}' Class: '#{a.class}'"</pre>

C# is a static typed language, all variables must indicate their type before instantiating an object. For example, when declaring a variable x of type string, you will be able to create an instance of string, only, there's no way to "reuse" x as an integer in the same scope. Try to compile the following example, it will fail:

public class RubyAndCSharp {
	
	public static void Main (string []args) {
		string x = "I'm string";
		System.Console.WriteLine ("a Value: '{0}' Class: '{1}'", x, x.GetType ());

		x = 10.0; // It will fail here: "error CS0029: Cannot implicitly convert type `double' to `string'"
		System.Console.WriteLine ("a Value: '{0}' Class: '{1}'", x, x.GetType ());
	}
}

Second, memory management. Both languages manage memory automatically: by default all memory is created and released automatically, there is no need to explicitly release or allocate memory, unless the programmer wants to do so. However, in C# there's a "difference" between types. There are two type categories: Value Type and Reference Type. The difference, related to memory use, is the way they work and the addresses in memory they use. Declaring value types automatically allocates memory, declaring reference types declares a pointer and the memory is allocated when the object pointed by the variable is instantiated. The Value Types are allocated in the stack and the Reference Types are allocated in the heap.

This difference is really important. Comparing two instances of objects with different “category”, one value type and one reference type, does not work, it just fails. Is like comparing an apple to an orange. Is comparing a value stored in the stack to a value stored in the heap. We can’t compare them without writing any extra code.

And this extra code means using the base class object as the pointer for different types, because both types, value type and reference type, are subclasses of object, in one way or another. Let’s try to compile the following example:

public class RubyAndCSharp {
	public static void Main (string []args) {
		object x = "I'm string";
		// Output: "a Value: 'I'm string' Class: 'System.String'"
		System.Console.WriteLine ("a Value: '{0}' Class: '{1}'", x, x.GetType ());

		x = 10.0;
		// Output: "a Value: '10' Class: 'System.Double'"
		System.Console.WriteLine ("a Value: '{0}' Class: '{1}'", x, x.GetType ());
	}
}

After this short (or long?) explanation we are ready to see talk about the methods.

Method Object.Equals()

Is used to test reference equality in reference types and bitwise equality in value types. For example:

public class RubyAndCSharp {

	class MyClass {
		public string Name { get; set; }
		public override string ToString () { return Name; }
	}
	
	public static void Main (string []args) {
		// object.Equals in Reference Types uses address memory
		MyClass myClass0 = new MyClass () { Name = "test" };
		MyClass myClass1 = myClass0;

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myClass0, myClass1, object.Equals (myClass0, myClass1));
		
		// Let's try again. This will return false. myClass1 and myClass2 are different instances
		myClass1 = new MyClass () { Name = "test" };

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myClass0, myClass1, object.Equals (myClass0, myClass1));
		
		// It doesn't matter myInt0 and myInt1 are different variables, equality will be true.
		int myInt0 = 1; 
		int myInt1 = 1;

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myInt0, myInt1, object.Equals (myInt0, myInt1));
	}
}

Operator equals (==)

Is, basically, a synonym of object.Equals, same rules apply.

public class RubyAndCSharp {

	class MyClass {
		public string Name { get; set; }
		public override string ToString () { return Name; }
	}
	
	public static void Main (string []args) {
		// == in Reference Types uses address memory
		MyClass myClass0 = new MyClass () { Name = "test" };
		MyClass myClass1 = myClass0;

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myClass0, myClass1, myClass0 == myClass1);
		
		// Let's try again. This will return false. myClass1 and myClass2 are different instances
		myClass1 = new MyClass () { Name = "test" };

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myClass0, myClass1, myClass0 == myClass1);
		
		// It doesn't matter myInt0 and myInt1 are different variables
		int myInt0 = 1; 
		int myInt1 = 1;

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myInt0, myInt1, myInt0 == myInt1);
	}
}

Operator Object.ReferenceEquals()

Pretty straightforward, tests reference:

public class RubyAndCSharp {

	class MyClass {
		public string Name { get; set; }
		public override string ToString () { return Name; }
	}
	
	public static void Main (string []args) {
		// Object.ReferenceEquals in Reference Types uses address memory
		MyClass myClass0 = new MyClass () { Name = "test" };
		MyClass myClass1 = myClass0;

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myClass0, myClass1, System.Object.ReferenceEquals (myClass0, myClass1));
		
		// Let's try again. This will return false. myClass1 and myClass2 are different instances
		myClass1 = new MyClass () { Name = "test" };

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myClass0, myClass1, System.Object.ReferenceEquals (myClass0, myClass1));
		
		// This will also return false.
		int myInt0 = 1; 
		int myInt1 = 1;

		System.Console.WriteLine ("object.Equals('{0}','{1}') = {2}", myInt0, myInt1, System.Object.ReferenceEquals (myInt0, myInt1));
	}
}

Colophon

Sometimes you will have to use an object reference to refer to both types, value and reference, if you are planning to compare their value you have to use the static method object.Equals(a,b). Using the operator equals (==) will always return false, because of the boxing/unboxing:

public class RubyAndCSharp {
	public static void Main (string []args) {
		string str0 = "hola";
		string str1 = "hola";
		
		object obj0 = str0;
		object obj1 = str1;

		System.Console.WriteLine ("Equals: {0}, Using ==: {1}, object.Equals {2}", 
		                          obj0.Equals (obj1), // True
		                          obj0 == obj1, // True
		                          object.Equals (obj0, obj1)); // True
		                          
		bool bool0 = true;
		bool bool1 = true;
		
		obj0 = bool0;
		obj1 = bool1;

		System.Console.WriteLine ("Equals: {0}, ==: {1}, object.Equals {2}", 
		                          obj0.Equals (obj1), // True
		                          obj0 == obj1, // False
		                          object.Equals (obj0, obj1)); // True
		                          
	}
}

Back to posts