Categorized | Computers, Tech Developments

First Lesson in Understanding C# Programming

Tags: , ,

The IDE

The ability to program computers is a desired skill which is rapidly growing as technology has become such a crucial and integral part of our lives. All of our electronic gadgets have logic embedded in them in some way or another and that is achieved through programming.

The first step to begin high level programming (Languages such as C#, Visual Basic etc) is having a developing environment. That may sound a little daunting but essentially it is a spruced up notepad which will allow you to manage, compile, run and bug check your code. If you visit the link here, you should download “Visual C# 2010 Express” which will allow you to: ” Build custom applications in Visual C#, a simple, powerful, type-safe, and object-oriented language that enables rapid application development with the expressiveness and elegance of C-style languages.”

If you are a student at a University or College then you should visit DreakSpark.com, Microsoft’s scheme for giving free software to students. This includes the complete Windows operating system as well as full versions of Visual Studio.

Download Page For The Free Program

Once Visual C# 2010 is installed, start by going to File > New > Project. Then under ‘Installed Templates’ select ‘Visual C#’ and then ‘Windows’ and then ‘Console Application’. Name your new project to “My First Program”, change the directory if you wish and hit OK.

Okay we now have our first program! Hurrah. You should be faced with the following code initially:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace My_First_Program

{

class Program

{

static void Main(string[] args)

{

 

}

}

}

 

Programming is a sequential art meaning it runs instructions one after another. The brackets indicate different areas of code. As you can see as part of the above code, “static void Main” is a part of “class program” and that is a part of “namespace My_First_Program”.

The main code for the program will be run inside of the main function so the code we put inbetween these brackets are the ones we are interested in currently. We can write anything we want here but lets start with something simple, outputting text to the console.

C# has in integrated Console class which will allow us to write to the console by writing the following: ” Console.WriteLine(“Hello World!”);” It is vitally important that you end all your single lines of code with a semi-colon. This tells the compiler that this is the end of the function / line of code.

Time to run the program and see if our code as worked! Go to Debug on the top menu and then > “Start Without Debugging”. Alternately type Ctrl + F5.

Do you see the text? If so you have successfully written a single line of code! Now let’s make things a little more complex. Declaring a Variable which will hold an inputted string then write it to the screen again.

Examine the code below:

 

string StringVariable;

StringVariable = Console.ReadLine();

Console.WriteLine(StringVariable);

On the first line we are declaring a new variable of type “string” called “StringVariable”. A variable can be thought of like a pigeon hole used to store information, it can be assigned new information or extracted.

 The IDE

In the second line we are assigning the string new data which the user will enter from the console.

In the third line we are writing our variable to the screen using the same function we used the first time round. Notice this time instead of manually typing text into the function we are passing in the StringVariable variable.

You now have a basic understanding of how to create a very simple program and output some text to the screen! Have a play around with the program and research different variable data types.

The Output From Our Program

Below if the final code for the program:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace My_First_Program

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine(“Hello World!”);

 

string StringVariable;

StringVariable = Console.ReadLine();

Console.WriteLine(StringVariable);

}

}

}

Leave a Reply

*

Follow Us

facebooktwitterpinterest
Spreaditfast

Featured Video

Poll

What was the Best Smartphone of 2012?
Nokia Lumia 920
Sony Xperia P
Samsung Galaxy S3
Apple iPhone 5
View Result
See all polls and results

Leave us a quick message!

  • Victor
    Great article on Linkedin! My profile looks much better now..
    14 February 2013
  • W Ich
    Great site!
    14 February 2013
Your Shout