C# Methods

A method is a sequence of statements and are declared within a class. Methods are useful if you need to use the same code over and over again, remember the word DRY (Don’t Repeat Yourself). In the text editor project you will see how methods can work.

Syntax

<Returntype> name (parameterList) 
{
Body Code
}

The method name should be something meaningful, and the same variable naming rules apply, so something like customer$name is not valid.

returnType – The returnType is the data type: int, string etc. Methods also return a value; if your method does not return a value then you must use the keyword void instead of returnType.

name – The name, as discussed earlier, is the method name.

ParameterList – This describes the type of information you want to pass on to the method. This is set in the brackets (or parentheses); if you set more than two parameters you separate them witha comma.  The ParameterList is optional.

Variables in Methods

You can create variables in methods; however before using a method you must declare it, and that variable can only be used within that method. This is called scope and the variables are called local variables.

Make a new Console Application and name it Methods:

Example

static void Main(string[] args) {
 
myname();

}
      
static void myname() {

Console.WriteLine("Hello World!");
Console.ReadLine();

}

In this application, we first make our method and call it myname and then in static void main the method myname is called. This is a simple method, there is no return type or any arguments to pass.

Passing Parameters

The above example is simple; it has no return value or arguments to pass. More complex methods have arguments and return values. In the next example we will create a short program which calculates product cost after VAT (Value After Tax) has been added. Comment out the previous code and copy this one, and put it above the previous method we created (myname):

static double VAT(double productCost, double currentRate)
{
 double cR = (currentRate + 100) / 100;
 return productCost * cR;
}

This time the method is called VAT with the data type double and two parameters: productCost and currentRate, which is the current VAT rate (20%). The data types for these are double because the result can be a floating point number, such as £43.50.

To calculate product cost after VAT has been added you use the percentage multiplier, so: (VAT rate + 100) / 100 e.g. (20 + 100) / 100 = 1.2. Remember you need the brackets to show which calculation you want to do first (in this case addition). Next we return the value, which is productCost multiplied by VAT rate.

Next, copy this (this goes in static void main):

Console.WriteLine("Enter product cost: ");
string pC = Console.ReadLine();

Console.WriteLine("Now enter current VAT rate: ");
string cR = Console.ReadLine();

var result = VAT(Convert.ToDouble(pC), Convert.ToDouble(cR));

Console.WriteLine("Total cost is {0:C}", result);
Console.ReadLine();

Now this is similar to what was done previously. In the first four lines we get the product cost and current VAT rate, and store them in the variables pC and cR. Then a new variable is made, called result, which calls the method VAT. We put our two arguments in there and convert them to doubles. The arguments must match, and the method VAT needs arguments (it will not except 0 arguments). When you type VAT( you should get a small tooltip showing you the data types and arguments this method excepts. Lastly, we print out the result to the console and format it so it has the currency symbol in front of it.

Run the program and test it: Enter a price and current VAT rate (20%), which will be calculated.

[ Continue ]