Functions without parameters or returned values:
Using system; Class myclass {static void show () {console. writeline ("function");} static void main () {Show (); // function console. readkey ();}}
Parameters:
Using system; Class myclass {static void show (string Str) {console. writeline ("is" + Str);} static void main () {Show ("function"); // is function console. readkey ();}}
Return Value:
Using system; Class myclass {static string show (string Str) {return "is" + STR;} static void main () {string S = show ("function"); console. writeline (s); // is function console. readkey ();}}
For a function, see return:
Using system; Class myclass {static int math (int x, int y) {return X + Y; return x * Y; /* cannot execute this sentence */} static void main () {console. writeline (Math (3, 4); // 7 console. readkey ();}}
Array parameters:
Using system; Class myclass {static int arrmax (INT [] ARR) {int max = arr [0]; for (INT I = 1; I max) max = arr [I]; return Max;} static void main () {int [] NS = {1, 2, 4, 5, 4, 3, 2, 1}; console. writeline (arrmax (NS); // 5 console. readkey ();}}
Parameter array (keyword Params ):
Using system; Class myclass {static int sum (Params int [] ARR) {int COUNT = 0; foreach (int I in ARR) Count + = I; return count ;} static void main () {int n = sum (1, 2, 3); console. writeline (n); // 6 N = sum (1, 2, 3, 4, 5, 6, 7, 8, 9); console. writeline (n); // 45 console. readkey ();}}
Reference parameters and output parameters:
Using system; Class myclass {static void proc1 (ref int num) {num = num * num;} static void proc2 (Out int num) {num = 100 ;} static void main () {int A = 9; proc1 (Ref A);/* The reference parameter (REF) must be initialized */console. writeline (a); // 81 int B; proc2 (out B);/* output parameters are similar to ref (but Initialization is not necessary ), is to get a value from the function */console. writeline (B); // 100 console. readkey ();}}
Overload:
Using system; Class myclass {static int add (INT N1, int N2) {return N1 + N2;} static string add (string S1, string S2) {return S1 + S2 ;} static void main () {console. writeline (add (111,222); // 333 console. writeline (add ("111", "222"); // 111222 console. readkey ();}}
Delegate (I think delegate is to treat functions as a type of means ):
Using system; Class myclass {delegate double myfuntype (double N1, double N2); static double add (double N1, double N2) {return N1 + N2 ;} static double Dec (double N1, double N2) {return N1-N2;} static double MUL (double N1, double N2) {return N1 * N2 ;} static double Div (double N1, double N2) {return N1/N2;} static void main () {myfuntype fun; fun = new myfuntype (ADD); console. writeline (fun (3, 2); // 5 fun = new myfuntype (DEC); console. writeline (fun (3, 2); // 1 fun = new myfuntype (Mul); console. writeline (fun (3, 2); // 6 fun = new myfuntype (DIV); console. writeline (fun (3, 2); // 1.5 console. readkey ();}}
Call external functions:
Using system; using system. runtime. interopservices; Class myclass {[dllimport ("user32.dll")] public static extern int MessageBox (int h, string M, string C, int type); static void main () {string STR = "Visual Studio 2008! "; MessageBox (0, STR," message prompt ", 0 );}}