2 <clause number="8.7.3" title="Methods" informative="true">
3 <paragraph>A method is a member that implements a computation or action that can be performed by an object or class. Methods have a (possibly empty) list of formal parameters, a return value (unless the method's <non_terminal where="17.5">return-type</non_terminal> is <keyword>void</keyword>), and are either static or non-static. Static methods are accessed through the class. Non-static methods, which are also called instance methods, are accessed through instances of the class. The example <code_example><![CDATA[
7 public static Stack Clone(Stack s) {...}
8 public static Stack Flip(Stack s) {...}
9 public object Pop() {...}
10 public void Push(object o) {...}
11 public override string ToString() {...}
17 Stack s = new Stack();
18 for (int i = 1; i < 10; i++)
20 Stack flipped = Stack.Flip(s);
21 Stack cloned = Stack.Clone(s);
22 Console.WriteLine("Original stack: " + s.ToString());
23 Console.WriteLine("Flipped stack: " + flipped.ToString());
24 Console.WriteLine("Cloned stack: " + cloned.ToString());
27 ]]></code_example>shows a Stack that has several static methods (Clone and Flip) and several instance methods (Pop, Push, and ToString). </paragraph>
28 <paragraph>Methods can be overloaded, which means that multiple methods may have the same name so long as they have unique signatures. The signature of a method consists of the name of the method and the number, modifiers, and types of its formal parameters. The signature of a method does not include the return type. The example <code_example><![CDATA[
33 Console.WriteLine("F()");
35 static void F(object o) {
36 Console.WriteLine("F(object)");
38 static void F(int value) {
39 Console.WriteLine("F(int)");
41 static void F(ref int value) {
42 Console.WriteLine("F(ref int)");
44 static void F(int a, int b) {
45 Console.WriteLine("F(int, int)");
47 static void F(int[] values) {
48 Console.WriteLine("F(int[])");
57 F(new int[] {1, 2, 3});
60 ]]></code_example>shows a class with a number of methods called F. The output produced is <code_example><![CDATA[
67 ]]></code_example></paragraph>