Hello World, C# style

Advertisements

Hello everybody,

Michael here, and today I thought I’d introduce a new programming language to this wonderful blog-C#. This will be the ninth new programming tool I’ve introduced on this blog, after:

  • R
  • MySQL
  • Java
  • Python
  • GitHub
  • HTML
  • CSS
  • Bootstrap

Yea, we’ve covered a lot of programming in the last 6-and-a-half years, but hey, nothing wrong with picking up a new language, am I right?

With that said, let’s introduce ourselves to C#

What is C#?

C# is a general-purpose, object-oriented (much like Python and Java) programming language developed by Microsoft in 2000 that runs on a little something called the .NET framework.

What is the .NET framework? The .NET framework is Microsoft-developed open-source software framework that acts as a runtime environment for C# (.NET also works with other languages too like PowerShell and F#).

Setting up C#

Now, how do we set up an IDE to play around in the C# sandbox? Since C# is a Microsoft-developed application, I’d suggest using a Microsoft-developed IDE for our C# development journey-Visual Studio. Here’s the link to download it; follow all necessary instructions to install the IDE-https://code.visualstudio.com/. Also, unless you want to develop C# applications for a giant (or even small) business, download the Community version of Visual Studio, as that IDE is free to use.

Hello world, C# style

Once you’ve installed the IDE, let’s go play around with it by doing the most cliche of introductory programming tasks-learning to print Hello world (yea I know hello world is a bit overrated for programming language introductions, but I think it works, so I’m gonna use it).

Now, once we’ve installed Visual Studio, let’s set up our new C# project.

In the Visual Studio interface, create a new project. You should see an interface that looks like this:

In the search bar at the top, type in console app and scroll down until you see something that looks like Console App (.NET Framework). Most importantly, remember to select the Console App option with the green C# icon, as we are working with C# (as you can see from this screenshot, you can also build Visual Basic and F# applications).

Click Next, which will take you to this screen:

All you really have to do here is give your project a name (I used MichaelsSandbox here) and click Next, which will take you to the following screen:

Assuming these are the default options, leave them as they are and click Create to create your first C# project. Also, copy this code onto the newly generated file:

using System;

namespace HelloWorld
{
    class MichaelsSandbox
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

After you’ve copied the code onto the new project file, save the file and click on the green-outlined triangle icon to run the script.

Interestingly, the output won’t show on the interface itself but rather on a separate command prompt. Granted, all the code really did was print Hello World! on a command prompt, but that was the whole point-to give you a very very basic introduction to the world of C#.

  • Don’t mind the process ... exited with code 0 message-it’s standard to see it when running your C# programs (though you can turn off this message)

The script, explained

Now, since this is my first C# lesson, let’s explain the script, shall we?

  • using System-this indicates that the script will use the System namespace. In C#, a namespace is a means of organizing your code into containers, which contain various classes and methods. The System namespace acts as the runtime environment for C#, as it contains various fundamental components for C# to run like fundamental data types and input/output mechanisms.
  • namespace HelloWorld-as I mentioned earlier, a namespace is a means of organizing your code into containers with their own classes and methods. In this script, my code will be in the HelloWorld “container”
  • class MichaelsSandbox-Much like in Java and Python, a C# class holds data and methods for a certain aspect of a program’s functionality
  • static void main (String[] args)-This is C#’s version of the main method, much like Java’s public static void main (String[] args) or Python’s def main():-interestingly, Java’s and C#’s main methods are nearly identicial
  • Console.WriteLine("Hello World");-This line prints the specific text onto the console/IDE, much like Java’s System.out.println() method or Python’s print() method.
  • Two things to note about the Console.WriteLine() method-you always need a semicolon at the end of the method call (much like in Java) and you always need to wrap the text value to be printed in double quotes (also much like Java).

Also, here’s the script in my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael

Leave a ReplyCancel reply