Skip to main content

Methods (C# Specification Part 7)

A method’s body specifies the statements to execute when the method is invoked.
A method body can declare variables that are specific to the invocation of the method. Such variables are called local variables. A local variable declaration specifies a type name, a variable name, and possibly an initial value. The following example declares a local variable i with an initial value of zero and a local variable j with no initial value.
using System;
class Squares
{
      static void Main() {
            int i = 0;
            int j;
            while (i < 10) {
                  j = i * i;
                  Console.WriteLine("{0} x {0} = {1}", i, j);
                  i = i + 1;
            }
      }
}
C# requires a local variable to be definitely assigned before its value can be obtained. For example, if the declaration of the previous i did not include an initial value, the compiler would report an error for the subsequent usages of i because i would not be definitely assigned at those points in the program.

A method can use return statements to return control to its caller. In a method returning void, return statements cannot specify an expression. In a method returning non-void, return statements must include an expression that computes the return value.

Static and instance methods

A method declared with a static modifier is a static method. A static method does not operate on a specific instance and can only directly access static members.
A method declared without a static modifier is an instance method. An instance method operates on a specific instance and can access both static and instance members. The instance on which an instance method was invoked can be explicitly accessed as this. It is an error to refer to this in a static method.
The following Entity class has both static and instance members.
class Entity
{
      static int nextSerialNo;
      int serialNo;
      public Entity() {
            serialNo = nextSerialNo++;
      }
      public int GetSerialNo() {
            return serialNo;
      }
      public static int GetNextSerialNo() {
            return nextSerialNo;
      }
      public static void SetNextSerialNo(int value) {
            nextSerialNo = value;
      }
}
Each Entity instance contains a serial number (and presumably some other information that is not shown here). The Entity constructor (which is like an instance method) initializes the new instance with the next available serial number. Because the constructor is an instance member, it is permitted to access both the serialNo instance field and the nextSerialNo static field.
The GetNextSerialNo and SetNextSerialNo static methods can access the nextSerialNo static field, but it would be an error for them to directly access the serialNo instance field.
The following example shows the use of the Entity class.
using System;
class Test
{
      static void Main() {
            Entity.SetNextSerialNo(1000);
            Entity e1 = new Entity();
            Entity e2 = new Entity();
            Console.WriteLine(e1.GetSerialNo());                        // Outputs "1000"
            Console.WriteLine(e2.GetSerialNo());                        // Outputs "1001"
            Console.WriteLine(Entity.GetNextSerialNo());          // Outputs "1002"
      }
}
Note that the SetNextSerialNo and GetNextSerialNo static methods are invoked on the class whereas the GetSerialNo instance method is invoked on instances of the class.

Comments