DEV Community

Cover image for From JavaScript to C#/.NET Pt.1
davidrpolk
davidrpolk

Posted on

From JavaScript to C#/.NET Pt.1

Let's Learn Some C#!

In this series I'll talk about what it's like to learn C#/.NET coming from a JavaScript background. My first focus in software engineering was JavaScript! Recently I've been expanding my skills and have ventured into learning C# and the .NET framework. So as I make my journey through .NET and C#, let's talk about similarities and differences between C# and JavaScript.

What is .NET?

In order to understand what exactly we're dealing with here, it's helpful to understand what the .NET framework is and what it's used for. .NET is a software framework developed by Microsoft. It was originally intended to be proprietary for development of software for the Windows platform, although early on, they changed course in favor of a community/open-source style development environment(today in the form of .NET Core/ASP.NET). For developing .NET applications, the 2002 version of their Visual Studio started compiling code into a Common Intermediate Language, instead of into machine code. Before this version of Visual Studio, IDEs(Integrated Development Environments) compiled to machine code. What that means is that languages had to be compiled into machine code that was specific to whatever computer you were developing applications for.

Example:
If you were to write an application for computer with processor X, you would have to write it differently in order to write the same application for processor Y and so forth..

By using a Common Intermediate Language, it's possible to write the application once and then compile it to the intermediate language, then have each platform compile that to its platform specific machine code. Thanks to this feature and the standardization of the Command Language Infrastructure(the specification for converting intermediate language to machine code), .NET is no longer solely for developing applications for the Windows platform. Developers can also develop for Mac OS and Linux systems as well mobile and web applications. Woot! Interoperability!
.NET also supports programming in multiple languages such as C#, VB.NET, C++, J#, F# and more.

Alt Text

C#?

No, it's not a musical note. It's a programming language! Most importantly, it's an object/component-oriented(class-based) and strongly typed language. I say these are important because it's where you will notice the most difference when comparing C# to JavaScript. While JavaScript is object-oriented, it isn't class-based and isn't strongly typed, unless you consider TypeScript. 

Class-Based

In a class-based language like C#, *everything* is part of a class. Would you like to create a function to do literally anything? You have to put it on a class. Would you like to declare a variable for a simple value? It goes on a class. Would you like write something to the console?....you guessed it...**goes on a class**. Coming from JS, this took a little getting used to, but it's not such a big change once you do it a few times. Here's an example of a simple program that writes to the console. Ignore the parts that don't make sense yet, as I'll cover them as the series goes on!

Example:

using System;

namespace hello_C_
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello C#!");
        }
    }
}

You may immediately notice a few foreign keywords: "using", "namespace", "static", "void". If you've used TypeScript(which is actually a great transition from JS to C#), some of these might already be familiar.


In the next part the series, I'll go into these keywords and more as we continue exploring C#!

do
{
Coding();
} while (Life.Length > 0)

Top comments (2)

Collapse
 
katnel20 profile image
Katie Nelson

My big leap was understanding Visual Studio with solutions, projects and all those other windows before I ever got to the .cs files.

Collapse
 
davidrpolk profile image
davidrpolk

I didn't mention it, but I started learning all this on a Linux platform. Unfortunately, there is no Visual Studio for Linux! There is Visual Studio Code with extensions, which is what I'm currently using. At some point I will partition my drive and install Windows because I really want to use the full Visual Studio. Visual Studio and .NET are like partners so I feel a little cheated in my current C# dev environment..!