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.
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.
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.
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
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!