Dela via


Methods (C# Programming Guide)

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it is called by the common language runtime (CLR) when the program is started.

Note

This topic discusses named methods. For more information about anonymous functions, see Anonymous Functions (C# Programming Guide).

Method Signatures

Methods are declared in a class or struct by specifying the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method.

Note

A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to.

Method parameters are enclosed in parentheses and are separated by commas. Empty parentheses indicate that the method requires no parameters. This class contains three methods:

abstract class Motorcycle
{
    // Anyone can call this. 
    public void StartEngine() {/* Method statements here */ }

    // Only derived classes can call this. 
    protected void AddGas(int gallons) { /* Method statements here */ }

    // Derived classes can override the base class implementation. 
    public virtual int Drive(int miles, int speed) { /* Method statements here */ return 1; }

    // Derived classes must implement this. 
    public abstract double GetTopSpeed();
}

Method Access

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

class TestMotorcycle : Motorcycle
{

    public override double GetTopSpeed()
    {
        return 108.4;
    }

    static void Main()
    {

        TestMotorcycle moto = new TestMotorcycle();

        moto.StartEngine();
        moto.AddGas(15);
        moto.Drive(5, 20);
        double speed = moto.GetTopSpeed();
        Console.WriteLine("My top speed is {0}", speed);
    }
}

Method Parameters vs. Arguments

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

public void Caller()
{
    int numA = 4;
    // Call with an int variable. 
    int productA = Square(numA);

    int numB = 32;
    // Call with another int variable. 
    int productB = Square(numB);

    // Call with an integer literal. 
    int productC = Square(12);

    // Call with an expression that evaulates to int.
    productC = Square(productA * 3);
}

int Square(int i)
{
    // Store input argument in a local variable. 
    int input = i;
    return input * input;
}

Passing by Reference vs. Passing by Value

By default, when a value type is passed to a method, a copy is passed instead of the object itself. Therefore, changes to the argument have no effect on the original copy in the calling method. You can pass a value-type by reference by using the ref keyword. For more information, see Passing Value-Type Parameters (C# Programming Guide). For a list of built-in value types, see Value Types Table (C# Reference).

Reference types are passed by reference. When an object of a reference type is passed to a method, the reference points to the original object, not a copy. Changes made through this reference will therefore be reflected in the calling method. A reference type is created by using the class keyword as in the following example:

public class SampleRefType
{
    public int value;
}

Now, if an object that is based on this type is passed to a method, the object will be passed by reference. For example:

public static void TestRefType()
{
    SampleRefType rt = new SampleRefType();
    rt.value = 44;
    ModifyObject(rt);
    Console.WriteLine(rt.value);
}
static void ModifyObject(SampleRefType obj)
{
    obj.value = 33;
}

This example essentially does the same thing as the previous example. But, because a reference type is used, the modification made by ModifyObject is made to the object that is created in the TestRefType method. The TestRefType method will therefore display the value 33.

For more information, see Passing Reference-Type Parameters (C# Programming Guide) and Reference Types (C# Reference).

Return Values

Methods can return a value to the caller. If the return type, the type listed before the method name, is not void, the method can return the value by using the return keyword. A statement with the return keyword followed by a value that matches the return type will return that value to the method caller. The return keyword also stops the execution of the method. If the return type is void, a return statement without a value is still useful to stop the execution of the method. Without the return keyword, the method will stop executing when it reaches the end of the code block. Methods with a non-void return type are required to use the return keyword to return a value. For example, these two methods use the return keyword to return integers:

class SimpleMath
{
    public int AddTwoNumbers(int number1, int number2)
    {
        return number1 + number2;
    }

    public int SquareANumber(int number)
    {
        return number * number;
    }
}

To use a value returned from a method, the calling method can use the method call itself anywhere a value of the same type would be sufficient. You can also assign the return value to a variable. For example, the following two code examples accomplish the same goal:

int result = obj.AddTwoNumbers(1, 2);
result = obj.SquareANumber(result);
// The result is 9.
Console.WriteLine(result);
result = obj.SquareANumber(obj.AddTwoNumbers(1, 2));
// The result is 9.
Console.WriteLine(result);

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

For more information, see return (C# Reference).

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.6.6 Methods

  • 10.6 Methods

See Also

Concepts

C# Programming Guide

Reference

Classes and Structs (C# Programming Guide)

Access Modifiers (C# Programming Guide)

Static Classes and Static Class Members (C# Programming Guide)

Inheritance (C# Programming Guide)

Abstract and Sealed Classes and Class Members (C# Programming Guide)

params (C# Reference)

return (C# Reference)

out (C# Reference)

ref (C# Reference)

Passing Parameters (C# Programming Guide)