Introduction to C#
If you want to know how to develop software using C# and you have little to no experience programming you've come to the right place. This is an introductory course to C# and .NET programming that is the first in a series that will show you how to use these dynamic cross-platform development tools.
In this course, we set the stage in two ways. First, you get an overview of the basic elements of computer programming and fundamental issues that face software development, and how .NET addresses those issues. Secondly, we set up our software development tools on Windows and a non-Windows platform. To test our development environments we create and run a simple C# .NET program.
- Get a foundational understanding of computer programming and .NET
- Learn how to set up a development environment
- Learn how to run a simple C# .NET program
This course is intended for anyone who wants to learn how to develop software using C#
To get the most out of this course, you should have some basic understanding of programming, but it's not essential; this course can also be taken by complete beginners.
I'm going to assume that most of you are using a Windows 10 PC, so that's the first one we'll look at. I've got a completely fresh Windows 10 installation here, and as you would expect, .Net Framework comes pre-installed. How do I know this? Well, there are several ways you can check which version of .Net is on your Windows machine.
The easiest way is to look for the .Net files. Open file explorer and navigate to the folder Windows and then Microsoft.NET. We can see two framework folders here, one ending with 64. This indicates we have two versions of .Net, one for 32bit architecture and one for 64-bit.
Most CPUs and operating systems be that Intel, AMD, Linux, Windows, and macOS, are 64-bit now, so 32-bit backward compatibility is more of a historical thing. Don't get me wrong, there are still plenty of 32-bit systems out there, as is evident from download options on various websites, so .NET does give you the option to target 32-bit when compiling an executable.
Within the Framework folder, we can see a bunch of folders that begin with the letter "v." These folders contain dynamic link library files, DLLs associated with the major versions of the .Net Framework. The most recent major version is four, so we'll go into that folder and select the Accessibility DLL, right-click on it, choose properties and go to the Details tab.
Here we can see the product version is 4.8, which indicates the .NET version. All the .Net 4.8 DLL files will have the same product version. Before moving on, some of you might be thinking, where's .Net version 3? .Net 3 isn't automatically installed, but you can activate it if required as a prerequisite for some software package you're installing. Go to turn on Windows features, and you can check the .Net framework 3.5 check box to enable it.
You can check for the installed version of .Net within the Windows registry. There two ways to do this. Open a command prompt as administrator and query the registry local machine key path of software, Microsoft, Net Framework Setup, NDP with the s option. This returns the keys and values underneath or within NDP. We can see the version value under v4 Full corresponds to the product version 4.8 we saw earlier.
Alternatively, you can open up Regedit and navigate to that key to check the version. Under v4 Client, you see the same values. As I said earlier, I'm using a brand new install of Windows 10, so if you don't have the latest version of .Net installed, just go to https://dotnet.microsoft.com/download/dotnet-framework and download the recommended version.
While we're here in the browser, let's download the Visual Studio IDE. Visual Studio is an integrated development environment, IDE, that makes writing and compiling code into executables much easier than it would be without it.
Visual Studio is a Microsoft product primarily designed to work with .Net and Microsoft programming languages like C# and Visual Basic, although it does support many others. I'll download and install the free Community edition. Actually, the Visual Studio installer is what gets downloaded, and the installer downloads and maintains the Visual Studio installation.
Once the installer is ready, I can select various workloads for the types of applications I want to create. Visual Studio can be used to develop a wide range of software, so there are many options, and we will only need a few of them. I'll select ASP.Net for web development, Azure for easy integration with Azure cloud services, and .Net desktop. I'll also check universal platform, .Net Xamarin mobile development, and .Net Core development, which we'll use to create our first application.
On the right, you can see a breakdown of components that make up the various workloads with the total space requirement at the bottom. You can quickly add and remove workloads as required with the installer. Across the top of the installer window, you can see options for installing individual components, additional language support, and changing the installation locations.
As you can see, there is a lot to download and install, so we'll come back to that when it's done.
Now I want to set up a development environment on a non-Windows machine, in this case, Ubuntu running KDE desktop. We won't be able to install Visual Studio as there isn't a version for Linux. There is Visual Studio for macOS, but it's a shadow of the Windows version.
What we can use, is Visual Studio Code. While not a complete IDE, VSCode is a very accomplished code editor with excellent compiler and debugger integration and language support extensions. I'll go to https://code.visualstudio.com/downloads and get the 64-bit deb version.
Once downloaded, open a console window and, as root run dpkg, the Debian package command with the "i" for install option. That's a long file name, so I'll copy it to avoid any typos. Yes, I could have just clicked Install Package, but if you're using a different Linux distro, that option might not be available.
After entering the root password, the extraction and installation go ahead, and we can see the Visual Studio Code icon has been added to the applications menu. Next, I'll create a projects folder, then a csharp folder, and an intro folder.
Within the intro folder, I'll open a console and type "code" that starts VS Code. Next, I'll install the C# language support extension by clicking on the extension button at the right and choosing C# for Visual Studio Code.
Funnily enough, but not really; Linux doesn't come pre-installed with .Net, so there's no point in checking the version. Let's go ahead and install the .Net SDK and runtime. As root, I'll get the appropriate package from Microsoft with the wget command – there are packages for different Linux distributions and versions. I'm running Ubuntu 20.04.
Then I'll install the package, again with dpkg and the "i" option. Next, install the .Net 5.0 SDK. We can see that the aspnetcore runtimes have also been installed, but if for whatever reason you need to install those runtimes separately, then use the same command, but swap out dotnet-sdk for aspnetcore-runtime.
As you can see, no changes have been made running that command. To confirm the installation, run dotnet --list-sdks, not skds. For runtimes, change SDKs to runtimes.
Let's go back to Windows. It looks like Visual Studio has finished installing, so we're ready to create our first C# program.
Check .NET version from a command prompt on Windows
reg query "HKLM\SOFTWARE\Microsoft\Net Framework Setup\NDP" /s
Install .NET on Ubuntu
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-5.0
Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard.