DEV Community

Scott Slatton
Scott Slatton

Posted on

Intro to C# for Javascript Developers, Part 1

Currently I’m in the last stretch of the Flatiron School’s code camp and at this point in time I’ve learned Ruby and Javascript as my primary programming languages. The time to start applying jobs is heading toward my cohort like a flaming meteor and imposter syndrome has been settling in on my peers quite harshly. When looking at job applications they see a bunch of technologies that they don’t know combined with abbreviations that they may not have seen or haven’t had the time to take a look at yet. I know that these are incredibly smart individual's that pick up concepts and start applying them at a level nearing light speed. So it's really just that they need to be exposed to these other technologies to realize that they have the core concepts down so well that transferring them will be no problem.

I believe in you!

With that said, I’d like to just try to pile on right now and show another programming language to my peers, one that they haven’t had their hands on yet. My goal isn’t to make anyone sink in their seats and feel discouraged and crushed from the overwhelming amount of information, but to show that if you’ve made it through to the final round of Flatiron (or if you can comfortably code in Javascript) that taking on another language is really trivial.

There's a saying that “Once you know how to code the rest is just syntax.” and I want to prove just how real that is. It’s not just a nice sounding platitude and I’ll prove it by showing side by side common functions and syntax of javascript with their C# counterparts.

C# Background

Don't cut yourself on this #edge

C# (pronounced See-Sharp) was released in 2002 to go alongside .NET framework from Microsoft. If you don’t like Microsoft for some reason, that’s okay, C# and Java are syntactically very similar so you can go between them fairly easily. The point of these languages was to increase productivity, be easy to use, and deploy. C# can be found in the Unity engine if you have an inclination toward Game Development (like me) and can also be used to make native mobile apps. They are strongly typed languages, meaning that you must explicitly declare what a variable’s data type is.

However, C# does have implicit conversion like Javascript which makes it very dynamic. C# is also a compiled language instead of interpreted, meaning that you must first turn your code into crunch-sized bytes before running the program. These programs are still interpreted top to bottom though just like JS so be careful when you are declaring and calling variables and functions. Another thing to note is that C# utilizes camelCase for local variables and parameters and PascalCase for class names and methods. Automatic garbage collection is another feature. Both C# and Javascript are based off of C so you'll see quite a lot of similarities.

Anatomy of the Class File

Alright don’t get scared off by that code up there I’m going to walk you through it real quick and show you that you already know what all of that is doing.

At the top it says “using System;” this is just an extension so it's just like "Import X from "./Xfile" for a library you'd see in JS or "Require Gem" in Ruby.

Move down we have "Namespace Test" which as you can guess namespaces all of the text in the file as "Test" to protect it from outside disturbances and from polluting the namespace of your application as a whole. You can call it whatever you want.

Continuing on we have "class MainClass" which as you can expect, starts the body for the "MainClass" class code. This can be changed to "User" or "Pikachu" this shouldn't be anything unexpected.

Line 5 is where the seemingly unfamiliar steps in but once I break it down you'll realize you know what every word does.

"Public" this is a declaration of the scope of the incoming function, it's either publicly available for use or Private.

"Static" is a keyword that declares that the method is global and can be called without creating an instance of the class.

"Void" is the return value of the function, as you may recall in JS Console.log() doesn't have a return value, it just prints to the console and is finished.

"Main()" is the name of the function and acts as the entry point of the application. Only one class in your application needs the Main method and you can specify which class' Main method you want to use in the IDE if you have multiple.

"string[] args" are the parameters for any arguments passed into the Main() function at runtime. This is completely optional to have in there and you can omit them if you don't plan to use any arguments during runtime. You can name "args" whatever you want, its just a parameter name.

Console.WriteLine("Hello World") is, as you'd expect, just Console.Log() or "puts" in JS or Ruby.

Hopefully it doesn't look so scary anymore.

Variable Declaration

The main difference between variable declaration in C# vs JS is that the type of variable needs to be declared immediately before the name of the variable is given.

So in JavaScript you'd see.

let greeting = "Hello"

But in C# the same variable needs to be declared as:

string greeting = "Hello"

Here's a short list of common datatypes used in C#, a longer list can be found here.

string greeting = "Hello World";
int number = 100;
double trouble = 10.2;
char grade = 'A';
bool beef = true;

You can also declare a variable and then assign it later, as shown below.

The ReadLine() function assigns the user's input value to the "name" variable and then logs the last concatenated string to us. Very basic, but this should be very familiar.

Looping Through An Array

In C# there are many different ways to declare array's. In the example shown above is personally my most comfortable way to make a new array but we can also declare how large an array is going to be before we use it, as a memory saving feature of the language. The for-loop in use is also nearly identical to what you would see in Javascript.

Hopefully this demystified a compiled language for you and will help you feel a little more comfortable when you see one of these languages on a job application.

In my next post I'll be going over MVC, Objects and Iterators. We're not done yet!

Top comments (1)

mahmoudthepeltist profile image

Heyy, great read! would love to see part two soon :D