static void Fun(int x, int y = 20, int z = 30) { Console.WriteLine("{0},{1},{2}", x, y, z); }
Fun(10); // y and z default to 20 and 30 Fun(1, 2, 3); // passing all three parameters Fun(100, 200); // x = 100, y = 200 and z defaults to 30
static void Fun(int x = 10, int y, int z) // cannot have mandatory parameters after optional parameter { // code }
Fun(100, z: 300); // 100 is passed to x and 300 to z
Parametername : value
static void Print(int x, int y) { Console.WriteLine("Values are {0}, {1}", x, y); }
Print(y: 10, x: 20);
static void Search(string sentence, string word, int spos = 0, int occurrence = 1) { // code }
pos = Search("How do you do", "do"); // start search from 5th position pos = Search("How do you do", "do",5); // start search from 0th position(taken by default) but look for 2nd occurrence. pos = Search("How do you do", "do", occurrence:2);