13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>C#</strong> LANGUAGE SPECIFICATION}public static readonly Color Red = new Color(0xFF, 0, 0);public static readonly Color Blue = new Color(0, 0xFF, 0);public static readonly Color Green = new Color(0, 0, 0xFF);public static readonly Color White = new Color(0xFF, 0xFF, 0xFF);8.7.3 MethodsA 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 return-type isvoid), 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 exampleusing System;public class Stack{public static Stack Clone(Stack s) {…}public static Stack Flip(Stack s) {…}public object Pop() {…}public void Push(object o) {…}public override string ToString() {…}…}class Test{static void Main() {Stack s = new Stack();for (int i = 1; i < 10; i++)s.Push(i);Stack flipped = Stack.Flip(s);Stack cloned = Stack.Clone(s);Console.WriteLine("Original stack: " + s.ToString());Console.WriteLine("Flipped stack: " + flipped.ToString());Console.WriteLine("Cloned stack: " + cloned.ToString());}}shows a Stack that has several static methods (Clone and Flip) and several instance methods (Pop, Push,and ToString).Methods can be overloaded, which means that multiple methods may have the same name so long as theyhave 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 exampleusing System;class Test{static void F() {Console.WriteLine("F()");}static void F(object o) {Console.WriteLine("F(object)");}static void F(int value) {Console.WriteLine("F(int)");}static void F(ref int value) {Console.WriteLine("F(ref int)");}34

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!