Beginning C#: Getting Started with C#

Introduction

Starting this month, I'm beginning a new set of articles in this column. "Beginning C#" is going to start right at the very beginning and teach C# as a language, right from the ground up. We'll start with the obligatory hello world example, and work our way up through variables, loops, decisions, structures, objects, and everything else that makes up the language.

Let's Get Started

So, what do you need to do to start programming in C#?

This is a question I see so frequently in the newsgroups, and on places like Lidnug and Stack Overflow, that it's something that really needs to be addressed.

Many people don't realise that you don't actually need Visual Studio, VS-Code, Sharp develop, or anything like that to actually write C# applications. Granted, these large application suites DO make things easier, but you can get along just fine with the .NET runtime being installed and a simple text editor, such as Notepad.

If you're working on Linux or an Apple Mac, all you need is "Mono" installed. You actually don't need the full Xamarin studio or anything like that.

The very first thing you need to make sure is that you have an up-to-date .NET runtime installed on your PC (if you're using Windows) and the current version of Mono installed if you're running a non-Windows system).

Unfortunately, I don't have time to cover installation on Linux/Unix/Mac in this column, but once you have the tools up and running, most of the language stuff we cover will work without modification.

On Windows, check your system hard drive (the one where your 'Windows' folder is) and look for a folder called 'Microsoft.NET' in your Windows folder:

Intro01
Figure 1: The main .NET install folder

If you have this folder on your PC, you're good to go. If you do not, you need to download and install the latest .NET runtime. You can get this from (at the time of writing) https://www.microsoft.com/en-gb/download/details.aspx?id=30653.

This URL may change over time, so your best course of action usually is to search for ".NET runtime download" in your favourite search engine.

Intro02
Figure 2: .NET Download portal

Simply click the large red download button and follow the instructions. You'll most likely also need to locate your downloads folder and run the installer you downloaded, in case your browser does not allow you to run downloads directly.

Once everything is installed head back to the location shown in Figure 1, that should now be present, and you should now see 'Framework' and 'Framework64' folders. (If you're working on a 32bit machine only, you may not see 'Framework64'.)

If you have 'Framework64', click on that to open its folder. Otherwise, just click 'Framework'. Whichever you choose, you should see something like what's shown in Figure 3:

Intro03
Figure 3: Inside the framework folder

In my case, I have .NET versions 2, 3, 3.5, 4, and 4.5 installed.

If you're observant, you'll have noticed that there is no 4.5 folder. That's because 4.5 is installed as an upgrade to v4.0, which, as a result, means the assemblies and tools are installed under v4.0

Click the v4.0 folder (or whichever your highest version is), and you should see something similar to Figure 4:

Intro04
Figure 4: MS .NET Tools

Within this folder is everything you need to write and compile .NET applications in C#, Visual Basic, and even JavaScript. (Yes, the .NET framework provides a tool to compile JavaScript into an EXE file.)

You can see the C# compiler in Figure 4; it's called 'csc.exe'. Open a command prompt and browse to the folder you have open. You can do this one of two ways:

  1. Go into Start->All Programs->Accessories and click the command prompt (Windows 7 and below), or press and hold the Windows key while pressing 'r', followed by entering 'cmd' in the run dialog, then pressing ok (all platforms).
  2. Right-click the white space to the right of the file listing in Explorer while holding down the Shift key, and then choosing 'Open command windows here' from the menu.

Whichever method you use, your goal is to end up with a command prompt open, pointing to your V4.0 .NET runtime folder:

Intro05
Figure 5: Command prompt open in .NET tools folder

From here, if you type 'csc' and press Return, the C# compiler should announce itself.

Intro06
Figure 6: Say hello to the 'csc' C# compiler

You can, if you want, try 'vbc' for Visual Basic and 'jsc' for the JavaScript compiler. For this series, however, we're only interested in the C# one.

At this point, we know we have a functional language compiler, but we're still on our system drive. Because of the various Windows file protections, you'll have a bit of a miserable time if you start compiling C# code here, so to make this more useful, we need to make sure that 'csc.exe' is available in the System path.

What's in a Path?

Under Windows, there is the concept of something called the "System Path" (a similar concept exists in other systems, too). The "System Path" is a list of disk locations to be searched when just a file name is specified for an application to be run.

For example, if you had "C:\folder1", "C:\folder2", and "C:\folder3", and your system path looked like "C:\folder1;C:\Folder2", any .exe files placed in 'folder1' or 'folder2' could be run from 'folder3' or anywhere else on the system just by typing 'appname.exe'. You could not, however, switch to 'folder2' and run an EXE file located in 'folder3' unless you typed the full location; for example, 'C:\folder3\appname.exe'.

By making sure that your .NET v4 folder is added to your Windows System path, you can run 'csc' (and the other tools) from anywhere on your system, and that includes any folders where you might be developing C# programs.

To add a folder to your path, you need to right-click your 'My Computer' icon and choose Properties:

Intro07
Figure 7: Right-click 'My Computer'

Upon doing this, you should see the computer Properties window open.

Intro08
Figure 8: Windows computer properties

The next thing you need to click is the "Advanced System Settings" entry, which will in turn open the following dialog box:

Intro09
Figure 9: The advanced System Settings dialog

And finally, click "Environment Variables" to open up the location where the "System Path" lives.

Intro10
Figure 10: The environment variables editor

You'll notice in Figure 10 that there are two 'Path' entries. The top one (marked by the green arrow) affects only the logged-in user's profile (in this case mine, shawty).  The bottom one (marked with the red arrow) affects every user on the system.

I personally use a number of different accounts for different purposes, so I use the system-wide entry. Whichever you use is up to you.

Double-click the 'Path' you want to modify and you'll open the editor for that variable:

Intro11
Figure 11: The 'Path' editor

Check that you don't already have your .NET folder in there already (it's much easier by copy and pasting the path into Notepad), and add the folder to the end of the path, if it's not already included.

Intro12
Figure 12: Adding our .NET folder to the path

Remember to separate your addition from what's already there with a semicolon ';' so that Windows knows this is a new folder and not part of an existing one.

Once you do this, click 'ok' all the way back out, and you should now be able to open a 'Command Line' anywhere on your system and run the compiler:

Intro13
Figure 13: csc.exe now runs from anywhere

To finish this first post off, we're going to create a simple hello world program. Open a command line window in any folder on your system that you want to work in, either by using the Start menu, Windows Run or the right-click + Shift method.

Then, type the following and press Return:

Notepad hello.cs

You should see that Notepad opens. When it does, type the following C# code into Notepad, aand then save it by using File->Save and return back to your already open command line.

using System;

class Program
{
   static void Main()
   {
      Console.WriteLine("Hello World!!");
   }
}

Back at your command line, type:

Csc hello.cs

And press Return. All being well, your program should compile, and produce an EXE file ready to run:

Intro14
Figure 14: Compiling and running your first C# program

I'll explain what we did next month, but for now try adding a few more "Console.WriteLine" or "Console.Write" statements and seeing what the difference is.

Remember, a big part of learning a new language is about experimentation, so don't be afraid to change things in the code we've done in this post, and see what happens. A very, very good way of learning how something works is learning how to fix it when it breaks.

Until next month.

Have Fun Playing!

Shawty



About the Author

Peter Shaw

As an early adopter of IT back in the late 1970s to early 1980s, I started out with a humble little 1KB Sinclair ZX81 home computer. Within a very short space of time, this small 1KB machine became a 16KB Tandy TRS-80, followed by an Acorn Electron and, eventually, after going through many other different machines, a 4MB, ARM-powered Acorn A5000. After leaving school and getting involved with DOS-based PCs, I went on to train in many different disciplines in the computer networking and communications industries. After returning to university in the mid-1990s and gaining a Bachelor of Science in Computing for Industry, I now run my own consulting business in the northeast of England called Digital Solutions Computer Software, Ltd. I advise clients at both a hardware and software level in many different IT disciplines, covering a wide range of domain-specific knowledge—from mobile communications and networks right through to geographic information systems and banking and finance.

Related Articles

Comments

  • RE: DR

    Posted by Peter Shaw on 01/30/2016 03:33am

    I'm please you like the idea. As I develop the posts then I will be building up your C# knowledge in small chunks. I always find it important to start small however, and as we progress the larger picture will emerge.

    Reply
  • RE: A good way to remind us these things are tools

    Posted by Peter Shaw on 01/30/2016 03:31am

    A very good comment Gavin, and one which many developers should always bear in mind. IDE's like Visual Studio are there mostly to increase the productivity output of those tools, but when your in Bear Grylls mode and only have your basic toolkit to survive with, it's always important to remember it can still be used without all the bells and whistles.

    Reply
  • A good way to remind us these things are tools

    Posted by Gavin on 01/26/2016 02:46pm

    In the day-to-day duties of the dev who spends a large % of time using a language such as c#, which can be a very large % indeed in enterprise development and becoming a way of life, can find writings like this a nice reminder that such things are but a tool in a very large box.

    Reply
  • DR

    Posted by J Engelbrecht on 01/17/2016 10:25pm

    For a start excellent. I am a begiiner at programming and runnng deep in age. Please be practical with your next lessons. Specificlally in the explanation how the various elements are interlocking with each other. We do not get handbooks showing the holistic picture if you want to program.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date