C# and Programming on Micorosft .Net Platform
I’ve decided to take a slightly different track in my blogging. I’m realizing more and more that sometimes crusty veteran software engineers need to get back to the basics and that’s just what I’m going to do. For the 2023 calendar year, I’m going to start an introductory course on C# and .Net. I’ll first start off with the basics such as control structures, arrays, classes etc. and I’ll end with more advanced concepts such as generics and newer additions to the language and platform. As always, I’ll add code to my github for concrete examples if anyone gets stuck. Stay tune for more!
“Hello World” in C#: Your First Coding Adventure
Welcome to the world of programming! If you’re reading this, you’re probably interested in learning how to code in C#. C# is a popular programming language that is widely used for creating desktop applications, mobile apps, games, and web applications. In this blog post, we’ll be walking you through the basics of C# programming by creating a simple “Hello World” console application.
Before we begin, you’ll need to have a few things set up on your computer. First, you’ll need to download and install Visual Studio, a popular development environment for C#. If you stumbled upon this blog post, you no doubt did some searching on the interenet for C# tutorials and I would guarantee you came across Microsoft’s Open Source editor for C# and the .Net environment called Visual Studio Code. While I find Visual Studio Code to be a fantastic text editor and great experience for building software, I do find the classic Visual Studio IDE is easier for beginners to pick up and start building great applications with due to it’s mature UI and WYSIWYG experience.
Step 1: Download Visual Studio Community Edition
Go to https://visualstudio.microsoft.com/ and download the free (as in free beer) version of Visual Studio called Visual Studio Community Edition. When you install Visual Studio, you will eventually see the prompt that follows. For those just getting started with programming and .Net, I suggest only selecting ASP.NET and web development and Azure development workload installations for getting started with Visual Studio Community Edition.
After selecting those two component workloads and wait for Visual Studio to finish installing. Once you have Visual Studio installed, you're ready to start coding!
Step 2: Create a new Visual Studio Solution
Open Visual Studio Community Edition. When the Visual Studio Start screen becomes visible, select Create a New Project, search for “solution” and select it from the search results. On the next screen, give our solution a name - call it “IntroToCSharp”
1. Click Create a new project
2. Search for solution
3. Give the new solution a name "IntroToCSharp"
Step 3: Create a new project
From your newly created solution, right click on the Solution Explorer node containing the name of your solution - IntroToCSharp. Select Add\New Project. In the New Project window, select Console App (.NET Core) from the list of templates. Give your project a name (e.g. “HelloWorld”) and select a location to save it. Click Create to create the project.
1. Right Click the solution title in the solution explorer and select Add New Project
2. Select Console App
3. Give the new Console app a name called "Lesson1.HelloWorld"
Step 2: Write the code
In the Solution Explorer window, open the Program.cs file. When you open this file, you will see it already has some code in it. This Program.cs file contains code that is designed to run as a console app and write text to a console - similar to the classline windows commandline and PowerShell or Bash terminal. In older versions of classic .Net Framework, the Program.cs file might contain a Main method, which is a classic function entry point for many other programming languages such as C++ and Visual Basic. The following is the code you should see in your Program.cs file. If you don’t see this code, you can clear out the file and add it here.
1 2 // This code will print the string "Hello World!" to the console. Console.WriteLine("Hello World!");
C# Code to print "Hello World" to a Console Window
Step 4: Run the program
To run your program, press the Start button or press F5 on your keyboard (If you’re old enough to know what a VCR is, it looks like the Play button on one of those!). A console window will appear, displaying the message “Hello World!”. And just like that you’ve just written your first C# program, Congratulations!
Running .Net Console app that prints "Hello World!" to a Console Window.
This is just a small taste of what you can do with C#. As you continue to learn and explore the language, you'll discover the many powerful features it has to offer. As always, Happy coding!