Methods

Methods are code blocks holding a number of statements.

An application calls the method to execute the statements, and specifies any related arguments. C# uses the context of a method to perform all instruction executions. The Main method serves as the entry point of all C# applications.

Review general method syntax below:

accesslevel datatype Name(parameters)
{
	statement;
}

SIGNATURES

The access level(e.g., public or private), modifiers(e.g., sealed or abstract) , return value, name, and parameters of a method form its signature. Parentheses enclose its parameters, and commas separate them; with empty space indicating it requires no parameters.

class MaxMin
{
	public int GetMax(int y, int z)
	{
		int maxnum;
		maxnum = (y > z) ? y:z;
		return maxnum;
	}
	...
}

ACCESS

Access a method with the following syntax:

ObjectName.MethodName();

List its parameters within the parentheses, and separate them with commas. Review an example of a method call below:

machine.IncreaseSpeed(10);

In the method definition, names and types of all parameters are specified, and all arguments passed in a method call must conform to the specified types, however, argument names do not have to conform.

PASSING: REFERENCE AND VALUE

In the passing of a value type to a method, a copy rather than an object is passed. Changes do not impact the original. In passing of a reference type, using the ref keyword, an object reference is passed meaning an argument indicating the object's location. Review the example below:

static void Main(string[] args)
{
	int argument;

	// Passed by value
	argument = 50;
	sqrVal(argument);
	Console.WriteLine(argument);

	// Passed by reference
	argument = 50;
	sqrRef(ref argument);
	Console.WriteLine(argument);
}

RETURNING VALUES

All methods without the return type void, can return a value using the return keyword. The value returned by the return statement must match the return type of the method. The return keyword not only returns a value, but also stops execution of the method. In the absence of a return statement, execution stops at the end of the code block, however, all methods not of void type must include a return statement.

When modifying an array with a method, returning the array proves unnecessary because any changes to an array will be observable by any code with a reference to it. C# passes all reference types by value, and the value of an array serves as its pointer.

ASYNCHRONOUS METHODS

The async feature allows for avoidance of explicit callbacks, manual code splitting across methods, and lambda expressions. The async modifier applied to a method allows the use of the await operator. Control reacts to await expressions by returning control to the caller, and suspending action in the method until a specified task completes.

The permitted return types of async methods include Task<TResult>, Task, and void. Void proves most useful in event handlers. Async methods with void returns cannot use await, and its caller cannot catch exceptions.

async Task<int> GoToTheWebAsync()
{
	HttpClient clientOne = new HttpClient();
	Task<string> getStringJob =
clientOne.GetStringAsync("http://www.windoze.com");

	PerformIndieWork();

	string urlStuff = await getStringJob;
	return urlStuff.Length;
}

SHORT METHODS

Methods with immediately returned expression results or a single statement are common. The => operator provides a shortcut for defining them. Void and async methods must have a body consisting of a statement expression. Properties and indexers must be read only, and the get keyword is not permitted. Review an example below:

public Product this[long productID] => stock.FindProduct(productID);