What is C#?
  • Before writing your own program in C# you should know bit of background information. C# or C Sharp (as it is sometimes called) is part of Microsoft’s .NET framework and is very similar to Java and C++. The major difference is that C# allows higher level programming than C++ with an automatic garbage collection and auto memory management.

  • C# is mostly an object driven language with the qualities of C++ and Java with some additional features. These features are useful when implementing design patterns making it much simpler when working with dynamic object models.
  • C# introduces new features in error handling and allows users to share libraries across languages. This reduces learning time and a programmer’s communication time when moving from one .NET language to another.

So what resources do you need?

Any version of Microsoft Visual Studio will do. However, as a beginner I would recommend an Express version of Visual Studio (it’s free). Now if you are going to be dedicated to learning C# then I would advise versions higher than Express, but it can get expensive. Each version of Visual Studio is different, for example,  if you wanted to create a Windows 8 metro styled application then you would need to use Visual Studio 2012. It’s best to know beforehand what kind of application you want to create so you don’t end up wasting time installing Versions of Visual Studio you don’t need. For this demonstration, I’m using Visual Studio 2010.

Before installing Visual Studio, I suggest you have these system requirements:

  • At least 2 GB of RAM
  • 3 GB Free space
  • A minimum of 1.6GHz processor
  • 5400 RPM hard disk drive

(For a full list of system requirements for any version of Visual Studio you can visit Microsoft’s website; the full requirements for Visual Studio 2010 are here).

If your system has lower specs you can still install Visual Studio, however, your system may be continually unresponsive, slow and unusable. Most of these specifications can be checked by running “MSinfo32” from the command prompt. Overall, the install does take some time as features and updates are downloaded while the installer is running. Note: A reboot will be  required to finalize the installation.

Write your first console application in C#

Install Visual Studio. Check. Now, it’s time to start creating your first program/application.

1. First, fire up Visual Studio 2010

Close down all other tabs and/or programs so that your computer is at optimal functionality for this process.

2. File > New Project

Create a project to start writing your program.

(This has to be done as each project will be stored in its own folder usually located in the Projects folder under Documents/Visual Studio 2010/).

3. Expand Visual C# and select Windows, then choose the Console Application Template

There are many types of templates to choose from. In this demonstration I’ll  be creating a very basic console application. Let’s name the console application “My First Program”.

4. You should now have a screen in the middle pane of Visual Studio that displays the contents of Program.cs like the image below:

eimagine-300x265

5. Now that the template is created and ready – it’s time to start writing your code!

Create a very basic console application that displays the name of the application and exits once you press a key. At the top of the file  write a brief description about your program:

*Now, remove all the contents from Program.cs. This way I can explain all the steps.

Our first application in C#…If you notice I use “//” at the beginning of our description, this is to enable comments in our code. Comments are mainly used to provide a description about a specific function so anyone who examines the code is aware of how it is compiled

6. Adding the required namespaces

Namespaces are required as .Net framework uses namespaces to organize its many classes. The first namespace we will declare is “system”.

Then we declare the namespace of our application.

7. Defining a class

When creating a console application it must contain a method which controls the execution of a program. Firstly,  you’ll need to create the class and then add a “Main” method inside our class. I’m using the method “Main” as it is a static C# reference method that is used in a class.

After specifying the namespaces and the classes your code should look like this:

8. Check your code!

*Note: Make sure that your code is the same or you may not be able to run your console program and I wouldn’t suggest using debugging at this stage.    

Now if you press “F5” your first program will compile and display a console like below:

eimagine2-300x139

9. You have finished your first program!

It’s time to start planning to build your own customized application. Have a play with Visual Studio you will get the hang of it after a while. If you don’t want to build a console application then have a go at a Windows Forms application, it allows you to design your program visually.

There are many articles around the web that explain C# in full detail, Here is a great resource about C#. It provides you with links so you can build up your knowledge to become an expert in C#.

 

Like this post? Share it!