Archive for the ‘c# vs Ruby’ Category

Subtle C# vs Ruby Inheritence Difference

This subtle differences in ruby and C#’s inheritence semantics bit me in the ass.

In Ruby:

class A
  def foo
    bar
  end
  def bar
    "in a"
  end
end

class B < A
  def bar
    "in b"
  end
end

Calling “B.new.foo” will output “in b”. I assumed this is how method overriding worked in C#, but that was a mistake. The same example in C# would output “in a”, because the `bar` referenced in A is not a message (like it is in ruby,) the compiler just sticks in the reference to the locally defined function.  An hour, a fistful of hair, and a call to a good friend (thanks AJ) later, I came to realize that in order to get the same kind of behavior, you have to use the “virtual” and “override” keywords in your classes’ method signatures.

This is a great article that explains it all: C# Overriding (virtual, override)

Posted: August 16th, 2009
at 8:36pm by Aaron Blohowiak


Categories: c# vs Ruby

Comments: 1 comment