C#+programming02 - Methods Methods So Far Methods We have...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Methods Methods So Far C# Programming © Rob Miles 2008 59 Methods We have already come across the methods Main , WriteLine and ReadLine . Main is the method we write which is where our program starts. WriteLine and ReadLine were provided by the creators of C# to give us a way of displaying text and reading information from the user. This is what methods are all about. Your programs will contain methods that you create to solve parts of the problem and they will also use methods that have been provided by other people. In this section we are going to consider why methods are useful and how you can create your own. Methods So Far In the glazing program above we spend a lot of time checking the values of inputs and making sure that they are in certain ranges. We have exactly the same piece of code to check widths and heights. If we added a third thing to read, for example frame thickness, we would have to copy the code a third time. This is not very efficient; it makes the program bigger and harder to write. What we would like to do is write the checking code once and then use it at each point in the program. To do this you need to define a method to do the work for you. Method and Laziness We have already established that a good programmer is creatively lazy. One of the tenets of this is that a programmer will try to do a given job once and once only. Up until now all our programs have been in a single method. The method is the block of code which follows the main part in our program. However, C# lets us create other methods which are used when our program runs. Methods give us two new weapons: 1. We can use methods to let us re-use a piece of code which we have written. 2. We can also use methods to break down a large task into a number of smaller ones. We will need both of these when we start to write larger programs. Again, as with lots of features of the C# language, methods don't actually make things possible, but they do help with the organisation of our programs. Essentially you take a block of code and give it a name. Then you can refer to this block of code to do something for you. As a silly example: using System ; class MethodDemo { static void doit () { Console.WriteLine ("Hello"); } public static void Main () { doit(); doit(); } } In the main method I make two calls of doit . Each time I call the method the code in the block which is the body of the method is executed. In this case it contains a single statement which prints "Hello" on the console. The result of running the above program would be:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Methods Methods So Far C# Programming © Rob Miles 2008 60 Hello Hello So, we can use methods to save us from writing the same code twice. We simply put the code inside a method body and then call it when we need it. Parameters
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 57

C#+programming02 - Methods Methods So Far Methods We have...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online