Basic C# Programming: Test Your Knowledge

Test your knowledge of the absolute basics of C#.

If you started learning C# recently but you're still getting the syntax straight in your head, these exercises are for you.

If you get stuck anywhere, check the answer, hide it again and see if you can do the exercise from memory. Or Google the answer!

You can click the link below each question to reveal the answer.

A Note On Compilers

If you use Microsoft's free Visual Studio C# Express, C# is a lot about dragging and dropping. But even if you can drag and drop like a pro, you still need to understand that syntax of the language to get anything serious done.

That's where this page comes in.

These exercises were created using Visual Studio C# Express, but they should compile just as well with any C# IDE or compiler (SharpDevelop, Mono, etc).

1. Create a "Hello World" C# Program.

Create a basic C# console-mode program that simply outputs the text "Hello World".

Your program should wait for the user to press a key before the program exits.

An Interactive Program

Create a program that asks the user to enter an integer. If the integer is less than 10, print the message "This number is too small". If the integer is greater than or equal to 10, print "This number is big enough".

Hint: read a line and then use Int32.TryParse() to convert the text to an integer. There is also a Int.Parse method, but it crashes if it can't parse your text to an integer. Well OK, it throws an exception. So don't use it.

As always, if you get stuck, check the answer, hide it again and try to write it from memory.


Create a program that creates an array of five hard-coded floating-point values, then prints out just the second value.

Arrays and Loops

Modify the above program so that it uses a foreach loop to display all the values in the array, all on one line, each number formatted to two decimal places.

Hint: use the ToString() method of Double (also known as double!) to format your numbers.

Two Dimensional Arrays

A bit trickier, this one. Write an application that creates a two-dimensional array of strings, with two rows and three columns. Print the value in the second row and third column.

Hint: regular 2D arrays can be declared using [,] instead of []. You can then access the array using [x,y], where x and y are the indices of the elements you want.

When you initialize the array (put actual numbers into it), remember that each element is an array in itself.

If attempting this question causes you such mental anguish that you travel to Sri Lanka to become a Buddhist monk or nun, only to get caught up in the monk-on-monk interfactional fighting, once you return home you might want to check the answer, hide it again and see if you can write the program from memory.

Remember, a 2D array is basically just a table of values, unlike a 1D array which is a list of values. When accessing the array, refer to the row and then the column.

So there's no need tear your hair out. Just go easy on yourself.

Looping Through 2D Arrays

Create an application that uses two nested for loops to loop through the 2D array defined above and print the values.

Hint: Here's the outer loop:

for(int row = 0; row < 2; row++) 

The inner loop should use a loop variable called "col" (or whatever you like) and should loop through the columns.

Hard code the number of columns and rows.

Once again, this is one of the trickiest beginner's tasks in C#. If you can't work it out by now you know what to do. Check the answer ....

Remember to follow rigorous indentation. The code in your outer loop should all be indented one tab. The code in your inner loop should be indented another tab.

Good indentation hygiene is one of the keys to a healthy C# program.

Try to make the output look like a 2x3 table, like the following:

one     two     three
apple   orange  banana

Hint: tabs are a good way to make your strings line up.

Create Classes and Objects

First, create a main program as in the first exercise.

Next, define a new class in its own file. Call the class Car. Give it a single method called "Start". Make the method simply print "Car started!".

In your main program, create a new Car object and call its Start() method.

Your final program should simply therefore display the text "Car started!".

Classes are one of the fundamental building blocks of C#, and indeed all OO (object-oriented) programs.

Once you can master this stuff, you've really take a leap forwards.


Modify the above Car class so that it has an instance variable called name of type string Add a constructor that accepts a string parameter and sets the car's name using this parameter. Add a GetName() method that returns the car's name.

Finally, modify the main application so that it sets the car's name via the constructor, then prints the cars name (retrieving it using GetName()).

Your program should output this:

Car started!
My Car

.... where "My Car" is whatever name you give to the car.

Often you set instance variables in C# via Properties. But let's leave that for a more advanced tutorial!

While Loops

Write an application that asks the user to enter the number '5' and loops over and over until '5' is entered.

When 5 is finally entered, print "Got it!".

Use a while loop!

Switch Statements

Write a program that asks the user to enter an integer. If the user enters '1', print "Only one?". If the user enters '100', print "100? That's a lot!". If the user enters something other than these two numbers, print "Input not recognized.".

The program should use a switch statement.

Hint: you may need to look up switch statements on Google. Use the default clause to implement the case where the user doesn't enter '1' or '100'.

Do...While Loops

A while loop checks its condition before the first iteration of the loop. A do...while loop checks the condition at the end of the loop. This means there's always at least one iteration of the loop.

Write a program that asks the user to enter an integer, then gets the input from the user. The program should repeatedly ask the user to enter an integer until the user enters an integer greater than 10; then it should print "Integer greater than 10 detected!" and should end.

The program must contain only two Write or WriteLine statements!

Hint: use a do...while loop to enclose the 'prompt' (i.e. the text that prompts the user to enter the integer) and the bit that gets the user input.


Once you can do this lot from memory, you've mastered the absolute basics of C#. Congratulations!

Using just these concepts, you can really write a lot of stuff already. Especially if you use Visual Studio C# Express and get used to dragging, clicking and dropping things!

If you found this page useful, please consider clicking one of the buttons below to recommend this page on Facebook, Stumbleupon or whatever. Thanks :)

(If you don't know what Stumbleupon is and you hate Facebook, I'll let you off the hook ....)

You might also be interested in: Make Your Own MP3 Player in C#.