C# and Ruby

I’m thinking that the next few posts are going to focus on the similarities between C# and Ruby, as I work through my notes from being onboarded into a project using C# and .NET.

One of the main differences is that C# is a ‘statically typed’ language. It requires explicit declarations, and everything is checked and errors are found when it is compiled, which can lead to more reliable code. Ruby, on the other hand, is dynamically typed, and is designed to be flexible. I’ll make a longer post about dynamic vs static later.
C# is definitely robust, and seems quite versatile in my limited exposure, but I could see how some things can become tedious (not my feeling, but that seems to be an opinion on the internet).

A really good example of this is in how you declare a variable

int integerVariable = 10;
string stringVariable = "string";
bool booleanVariable = true;

As you can see, in C# each variables type needs to be explicitly declared.

integer_variable = 10 
string_variable = "string"
boolean_variable = true

It is pretty nice that Ruby allows you to just name a variable, but I can see instances where defining the datatype before the variable name could come in handy, especially when you’re looking at an unfamiliar codebase.

I am going to give a few other quick examples so you can see the difference in the way these are laid out, nothing fancy, but I find it does help to see it written out

If – Else

C#

if (integerVariable > 5)
{
    Console.WriteLine("Greater than 5.");
}
else
{
    Console.WriteLine("5 or less.");
}

Ruby

if integer_variable > 5
  puts "Greater than 5."
else
  puts "5 or less."
end

For Loop

C#

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

Ruby

5.times do |i|
  puts i
end

I find it helpful to see what the two sets of code look like, hope you do as well! Next time I’m going to look at methods/functions, arrays, and maybe hashes? I haven’t decided yet.


Comments

Leave a Reply

Discover more from Brendan Bondurant

Subscribe now to keep reading and get access to the full archive.

Continue reading