C# Workshop – Methods

Standard

Methods

In all the previous examples there has just been one static method of Main in the Console application and have even used methods such as int.Parse. A method is a function used in a class and contains a series of usually related statements in a programme, they can optionally accept Parameters which are values that can be passed in or return a value.

Methods are something that do something in the programme and should be named accordingly so it’s clear what it does and it usually should only do one thing and allows you to avoid repeating yourself when writing a programme, to create a Method in dotnetfiddle.net enter the following:

using System;

public class Demo
{	
	public static int Addition(int value1, int value2)
	{
		return value1 + value2;
	}

	public static int Subtract(int value1, int value2)
	{
		return value1 - value2;
	}
	
	public static void Main()
	{			
		Console.WriteLine("Addition:" + Addition(9,2));
		Console.WriteLine("Subtract:" + Subtract(9,2));
	}
}

In this example Addition is declared as a public and static method and it takes two parameters – which are between the brackets and then returns the values added together and Subtract is similar but returns the values subtracted from each other.

Methods that can be written or declared can return a value but they don’t have to return a value – these values use void in place of a return type, and they can either accept parameters or not and it is also possible to have methods that have parameters that are optional but present with a default value when none is passed in.

An example of void and optional parameters in dotnetfiddle.net by entering the following:

using System;
using System.Text;

public class Demo
{	
	public static void Loop(string value, int loop = 1)
	{
		StringBuilder message = new StringBuilder();
		for(int i = 1; i <= loop; i++)
		{
			message.AppendLine(value);	
		}
		Console.WriteLine(message.ToString());
	}
	
	public static void Main()
	{			
		Loop("Hello");
		Loop("Hello World", 10);
	}
}

In this example, there is a public static Method that doesn’t return a value but it uses a string Parameter called value which must always be used, and a second int Parameter which is optional, this value is used to control a for loop inside the method. Another namespace is also used in the example for System.Text which contains the StringBuilder which is used here, an Instance or copy of this needs to be used – that’s what new does. Then the AppendLine Method of it is used to add a new line to the StringBuilder and when the loop is complete this is then converted to a string using an Extension Method called ToString, this is similar to how int.Parse was used before and the Loop Method is called with or without the loop value to show the different behaviour.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s