With which method does an object expire

Methods (C # Programming Guide)

A method is a block of code that contains a series of instructions. A program causes the instructions to be executed by calling the method and supplying any required method arguments. In C #, all statements are executed in the context of a method.

The method is the entry point of every C # application and is called by the common language runtime (CLR) when the program starts. In an application that uses top-level instructions, the method is generated by the compiler and contains all of the top-level instructions.


This article explains named methods. For information about anonymous functions, see Anonymous functions.

Method signatures

Methods are declared in a class, structure or interface by specifying the access level such as B. or, optional modifiers such as. B. or, the return value, the name of the method and the method parameters can be specified. These parts together form the signature of the method.


A method's return type is not part of the method's signature when it comes to method overload. However, it is part of the method signature when determining compatibility between a delegate and the method it references.

Method parameters are enclosed in brackets and separated by commas. Empty brackets indicate that the method does not require any parameters. This class contains four methods:

Method access

Calling a method on an object is similar to accessing a field. After the object name, add a period, the name of the method, and parentheses. Arguments are listed within the brackets and separated by commas. So the methods of the class can be called as shown in the following example:

Method parameters and arguments

The method definition specifies the names and types of any parameters that may be required. When calling code calls the method, concrete values, called arguments, are provided for each parameter. The arguments must be compatible with the parameter type, but the name of the argument used in the calling code (if any) does not have to be the same as the parameter name defined in the method. For example:

Passed by reference and passed by value

When an instance of a value type is passed to a method, the associated copy is passed by default instead of the instance itself. Changes to the argument therefore have no effect on the original instance in the calling method. Use the keyword to pass a value type instance by reference. For more information, see Passing Value Type Parameters.

When an object of a reference type is passed into a method, a reference to the object is passed. That is, the method does not receive the object itself, but an argument that specifies the location of the object. If you change a member of the object using this reference, the change in the argument is reflected in the calling method, even if you pass the object by value.

You create a reference type using the keyword, as shown in the following example:

Now when you pass an object based on this type to a method, a reference to the object is passed. The following example passes an object of the type to the method:

The example is essentially the same as the previous example and passes an argument as a value to a method. But because a reference type is used, the result is different. The change made in the parameter's field () also changes the argument's () field in the method. The method shows 33 as the output.

For more information about passing reference types by reference or by value, see Passing Reference Type Parameters and Reference Types.

Return values

Methods can return a value to the calling function (the caller). If the return type - the type listed before the method name - is not, the method can return the value using the keyword. A statement with the keyword followed by a value that matches the return type returns that value to the method caller.

The value can be returned to the caller by value or, starting with C # 7.0, by reference. Values ​​are returned to the caller by reference when the keyword is used in the method signature and follows each keyword. The following method signature and return statement indicate that the method returns a variable named by reference to the caller.

The keyword also kills the method. If the return type is, a statement with no value is still useful to stop the method from executing. Without the keyword, the method terminates when the end of the code block is reached. Methods with a return type other than void must use the keyword to return a value. The following two methods use e.g. B. the keyword to return whole numbers:

To use a value returned by a method, the calling method can use the method call itself anywhere that a value of the same type is sufficient. You can also assign the return value to a variable. For example, the following two code examples produce the same result:

Using a local variable, in this case, to store a value is optional. It can improve code readability, or it may be necessary when you need to store the original value of the argument for the entire scope of the method.

To use a value returned by reference from a method, you must declare the ref local variable if you want to modify that value. For example, if the method returns a Double value by reference, you can define it as a ref local variable using code like the following:

Returning a multidimensional array from a method that modifies the content of an array is not necessary if the calling function passed the array to. You can return the resulting array for the functional flow of values ​​or for stylistic reasons. However, this is not necessary because C # passes all reference types by value and the value of an array reference is a pointer to the array. In the method, changes to the contents of the array are taken into account by any code containing a reference to the array, as shown in the following example:

See return for more information.

Asynchronous methods

The Async feature lets you call asynchronous methods without using explicit callbacks or manually splitting your code across multiple methods or lambda expressions.

When you mark a method with the async modifier, you can use the await operator in the method. When an await expression is reached in the asynchronous method, control is returned to the caller and execution of the method is suspended until the expected task is completed. When the task is complete, execution in the method can continue.


An asynchronous method is returned to the caller when it either encounters the first expected object that has not yet completed, or when it reaches the end of the asynchronous method.

An asynchronous method usually has a return type of Task , Task, IAsyncEnumerable , or. The return type is mainly used to define event handlers, where a return type is required. An asynchronous method that returns cannot be waited for, and the caller of a method that returns void cannot catch any exceptions that the method throws. Starting with C # 7.0, an asynchronous method can return any task-like type.

The following example is an asynchronous method with a return type of Task . contains a statement that returns an integer. Because of this, the method declaration must have the return type. Because the return type is, evaluating the expression to an integer, as the following statement demonstrates:.

The method is an example of an asynchronous method with the Task return type. It follows the method, and since it is expressed on a single line, you can omit the keywords and. Since is an asynchronous method, the task for calling must be awaited, as shown in the following statement:.

An asynchronous method cannot declare ref or out parameters, but it can call methods that have such parameters.

For more information about the asynchronous methods, see Asynchronous Programming with Async and Await and Asynchronous Return Types (C #).

Expression text definitions

There are often method definitions that simply return the result of an expression directly or have a single statement as the text of the method. A syntax abbreviation is available to define such methods using:

If the method returns or if it is an asynchronous method, the text of the method must be a statement expression (like lambdas). Properties and indexers must be read-only. Additionally, do not use the accessor keyword.


An iterator performs a custom iteration through a collection, e.g. B. a list or an array. An iterator uses the yield return statement to return each element one at a time. When a yield return statement is reached, the current position is saved in the code. The next time the iterator is called, execution will restart from that position.

You invoke an iterator in client code using a foreach statement.

The return type of an iterator can be IEnumerable, IEnumerable , IEnumerator, or IEnumerator .

See Iterators for more information.

C # language specification

See the C # Language Specification for more information. The language specification is the authoritative source for the syntax and usage of C #.

See also