Code Clock Summer School of Programming: Workshop 2 Programming With C#
Code Clock Summer School of Programming: Workshop 2 Programming With C#
time.to.code
C#
Workshop 2
Programming with C#
TUTORIAL 1
BLOCKS
1st August 2016
Tutorial 1
EXERCISE 1
The tasks in the first part of this section are step by step instructions to guide you in the practical
application of the theory of programming. This is followed by a number of tasks for you to complete
by applying the knowledge that you have previously gained.
Step 2: Click on the File tab at the top left and then select New > Project. Note the key combination
of Ctrl+Shift+N can also be used as a shortcut.
Page 2
Tutorial 1
Step 3: A new pop up window will open in which we select our project type. On the left hand side
from the Templates drop down menu choose Visual C# and then select Console Application as
shown below. Then enter a suitable name for your project such as Prac1Ex1 and click OK.
Step 4: Your project has now been created and a class file called Program.cs will open. Note:
there may already be some code in the window and you will only need to type two of the lines
below. Console.WriteLine simply displays the text between the quotation marks in the
console window.
Page 3
Tutorial 1
Step 5: Save your work (CTRL+S) and then press the Start
button on the toolbar. Your
program will run and a console with open showing the results. Press any key to close the console.
Step 2: Now you will need to declare a variable of type String to hold the users name. It is good
practice to give your variables names that accurately describe what they are to avoid any confusion.
Console.ReadLine is used to take user input and will accept characters which are typed until
the user presses enter. The characters are assigned to the variable myName.
Step 3: Using another instance of Console.WriteLine you can display your name by referring
to the variable used to store it.
Page 4
Tutorial 1
Now you have experience with basic user input and output.
TASK 3: COMMENTS
Comments are annotations or notes written by the programmer which explain the code to another
user and make it easier to understand. They can be very significant to a human but are generally
ignored by the computer in the compilation of the program. In terms of our first program, comments
may not seem necessary but as projects get larger they become more important and it is good
practice to get into the habit of using them. Examples are shown below but be sure to try it out for
yourself.
Page 5
Tutorial 1
EXERCISE 2
TASK 4: GETTING TO GRIPS WITH VARIABLES
Now it is time to test some of the knowledge you have gained involving creating variables and data
types.
Create a new project (C# Console Application) as shown in Exercise 1. Name it Prac1Ex2 and click
Ok.
In your program create three int variables and call them width, height and area. We will need to
make use of the Console.ReadLine( ) to initialise the height and width variables with values
input by the user. However as the ReadLine() accepts characters rather than numbers we will
need to convert our input to an int so that we can perform mathematical operations on it. We
simply use a method that has been built in type conversion method called Convert followed by a
period and ToInt32 specifies what type we want to convert to.
This will assign the next number entered to our variable width. Do the same for height so
both the variables have been assigned values. Find the area of the shape by multiplying the
two variables and storing the result of the operation in the variable area. Finally print area
out using Console.WriteLine(area);
Example Output
Page 6
Tutorial 1
Page 7
Tutorial 1
EXERCISE 3
TASK 9: METHODS
Step 1: Create a new project (C# Console Application) as shown in Exercise 1. Name it Prac1Ex3
and click Ok.
Step 2: In the program class after the closing bracket for the main method you will need to define
two methods called messageOne and messageTwo. These methods will be public and have no
return type i.e. be void and accept no parameters. Each method will simply output a message to the
console such as:
The static keyword placed after the access modifier indicates that the method belongs to the class
which defines it in this case our Program class.
Step 3: Now call your two methods in your main method by using their names followed by
parentheses ().
Example Output:
Page 8
Tutorial 1
Step 2: Define two methods in the new class. The first will be a public method and have no return
type or parameters, call it publicMethod(). The second method will be similar but will be
private and thus called privateMethod(). Remember to add the static keyword after the access
modifiers to indicate that the method belongs to the EncapsulationTest class.
<access modifier> static <return type> <method name> (parameters)
{
Method body
}
Step 3: Output a message from each of your methods making sure to differentiate between which
method is being called e.g.
Step 4: Call both of the methods in the main method. You should receive an error when trying to
call the privateMethod( ). This is because of the accessibility level we assigned to the method
by using the private keyword. How can this issue be solved without changing the access modifier of
the method to public?
Page 9