DEV Community

Cover image for C# for the JavaScript Developer
Jade Doucet
Jade Doucet

Posted on

C# for the JavaScript Developer

As a new JavaScript developer, if you're still new to the developer scene as a whole, learning a new programming language can be pretty daunting. Lots of job postings are requiring so much experience, and it all feels so overwhelming, but don't worry, you're not alone. We've all felt this way at some point. In this post I'll be going over a few C# basics to prove to you that learning a new programming language is not as scary as you thought!

Basics

So, lets cover a few things that you may or may not be familiar with. When declaring variables in JavaScript, we have so much freedom without realizing it. We can declare a string, reassign it to a number, then to a Boolean. In many other Object Oriented programming languages, you actually need to specify your data type of your variable before using it, and declaring it may actually be how you even declare that variable! If that sounds confusing, this should clear it up:

In Javascript:
var myName = "Bob";

In C#
string myName = "Bob";

This is an example of declarative programming, which is essentially just specifying the data type of a variable before assigning a value to it. C# also provides a var keyword, which is definitely another way to assign variables, but keep in mind, that data type cannot be changed.

In JavaScript:
var myName = "Bob";
myName = true; // myName is now equal to a Boolean, this is fine

In C#:
var myName = "Bob";
myName = true; // This will throw an error

Data Types

Now that we are familiar with data type declaration's lets go over some of the data types available to us.

  • bool -> Boolean - will only be true or false
  • char -> character - can hold a single 16 bit unicode character ('A' -> 'z')
  • int - > integer - maximum number of 2,147,483,647 -> int.MaxValue will provide this number as well
  • long -> allows for large non-floating point numbers
  • decimal -> also allows for large numbers
  • float -> 32 bit number, larger numbers with up to 7 decimals of precision
  • double -> 32 bit number, even larger numbers with up to 15 decimals of precision

By executing MaxValue on each of these numeric data types can show you their maximum size. This is actually important to keep in mind if you're working with numbers often. It can be easy for some programs to surpass the MaxValue of an int data type.

Some Familiar Territory

So often, we use console.log() to see our errors, values, or whatever. Luckily, C# provides us with Console.WriteLine(), which is essentially the C# equivalent. With this in mind, lets get to some familiar things.

Incrementing:
i++;
++i;
i--;
--i;
i += 5;
i-= 5;

And all of those other fun ways of incrementing in JavaScript using multiplication and division.
Something different here, is ++i vs i++. We are accustomed to i++, and it's also commonly used in C#, but they are slightly different. Here, check it out:

  1. int i = 0; // declare our integer
  2. Console.WriteLine(++i); --> Outputs 1 - As Expected.
  3. Console.WriteLine(i++); --> Outputs 1 - What?!
  4. Console.WriteLine(i); --> Outputs 2

Since the items being written into the console are read left to right,
on line 2, i is incremented by one, then the value of i is read.
On line 3, i is read and printed to the console, and then it's incremented by 1. As you can see on line 4, the value of i is actually affected, but it wasn't changed at the time it was being printed to the console. This can be an important factor when working with incrementing in the console.

Hopefully this shows you that going from JavaScript to another programming language isn't so bad, but if you're not convinced, stick around for next week. I'll extend this, or talk about a new framework which allows for WEB DEVELOPMENT with C#! Thanks for reading!

Top comments (0)