C# As A Second Language: Basic Exercises 01

Link to the exercises I’ll be working through from the Breaking IT Down Blog.

When I began learning JavaScript, I couldn’t imagine learning something more challenging. I am not a person that naturally took to programming, but I am very stubborn. I was able to get through the difficult beginning and now I can look at some JS code and know what is going on there and how to fix it if it’s broken.

Over the years I have thought I might benefit from learning another language, but the idea of starting from scratch always struck fear into my heart. Eventually though, I worked up enough courage to start the basic C# tutorial on CodeAcademy.com, and I was absolutely shocked and delighted to find that much of the C# syntax was almost identical to JavaScript.

I’d like to share this newfound joy I have of not being afraid of something I didn’t understand. So, I’m going to publish a short series of articles as I work through some basic C# exercises found on the Breaking IT Down blog. They are a great introduction to some of the common things C# is used for, and they have helped me to feel much more comfortable writing C#.

One note though, before I get to the code:

Though C# is built around Object Oriented Programming, I won’t be using OOP principles in the code examples in this article. I’m using these exercises to demonstrate the differences and similarities in syntax between JavaScript and C#, and I’ll be refactoring the code later to be more in line with OOP’s practices.

With that disclaimer out of the way, let’s dive in to the exercises!

Exercise One

The tasks in each exercise will be in bullet points, and each task will have its solution directly below it in a code block.

  • Create a string variable to store a message to the user asking them for their full name
string message = "What is your name?";

Already there are a lot of similarities to JavaScript in declaring this variable, but with some notable differences. Where in JavaScript we would type let or const, here string is declaring the type of the variable. Other types in C# include int, char, bool, and float. Read more about C# types here.

  • Use the variable to ask the user for their full name and store the user’s input in a different variable
string userName = Console.ReadLine();

So we’re writing a line to the console with Console.WriteLine(), similar to console.log in JavaScript, and then we’re reading the user’s response into the string variable userName with Console.ReadLine(). Read more about the Console in C#.

  • Display their name with all letters as upper case and remove any empty spaces on the right of their input
string userNameUpperTrimEnd = userName.ToUpper().TrimEnd();
Console.Writeline($"Hello {userNameUpperTrimEnd}!");

Another similarity to JavaScript, C# also lets you use the TrimEnd() and ToUpper() methods, though notice the different casing. C# uses PascalCasing for its methods, not camelCasing. Read more about casing in C#.

Also notice the string literal interpolation syntax. When a string begins with $ variables can be placed inside the {} . In JavaScript the exact same outcome is possible with backticks instead of quotes: `Hello ${userNameUpperTrimEnd}!`Another valid C# syntax would be this:

Console.WriteLine("Hello {0}!", userNameUpperTrimEnd);

Read more about string interpolation in C#.

  • Display their name with all letters as lower case and remove any empty spaces on the left of the input
string userNameLowerTrimEnd = userName.ToLower().TrimStart();
Console.WriteLine($"Hello {userNameLowerTrimEnd}!");

Similar to the previous task.

  • Display the length of their name as they entered it but removing any empty space on either side of the input
int userNameLength = userName.Trim().Length;

One important thing to note here: there is some implicit type conversion going on when we print userNameLength to the Console. We need a variable of type int to hold the number of characters in the user’s name, but that int is converted to a string when it is printed to the Console. What gives? This is supposed to be a strongly typed language and yet here we are converting types on the fly like in JavaScipt!

In C#, if there is NO chance that converting a variable from one type to another will result in loss of data, then that variable can be implicitly converted. But if there is even the slightest chance that a conversion would cause data loss, then conversion needs an explicit declaration in the code.

So an int can become a string because any valid int can be represented as a string, but not all valid strings can be valid ints. For example, 100 can become "100" without issue, but "100A" cannot be converted to a number without losing some data (removing the "A" ).

This really gets to the heart of one of the main differences between C# and JavaScript. Read more about type conversions in C#.

  • Display the location of the first space in their name, ignoring spaces on either side of the input
int firstSpaceIndex = userName.Trim().IndexOf(" ");

We can see another C# method working almost exactly as a comparable JavaScript method here.

  • Display the location of the last space in their name, ignoring spaces on either side of the input
int lastSpaceIndex = userName.Trim().LastIndexOf(" ");

And again, almost identical to JavaScript but for the changes already discussed.

So, there is the first exercise from the worksheet. I hope this helps dispel any fears you may have of learning a new language. It was daunting for me from the outside, but once I actually started to look into the details of how it is written, C# got a whole lot more welcoming.

Github repo for this project.

I’m a web developer focusing on interactive projects using React and Nodejs.