Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-namedandoptional (named and optional parameters) |
1.A, example (sample) back to top |
Named and optional parameters
C # 4.0 supports named and optional parameters. This program demonstrates how to declare a method with a named and optional parameter, and how to invoke the method by using the default value of the parameter and by explicitly naming a specific parameter.
1.B, sample code back to top |
1.b.1, Program.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacenamedandoptional{//This program demonstrates how to use a method that declares a named parameter and an optional parameter.//and how to explicitly use it while calling the method//named parameters and optional parameters. classProgram {//methods with named parameters and optional parameters Public Static voidSearch (stringNameintAge = +,stringCity ="Pueblo") {Console.WriteLine ("Name = {0}-age = {1}-city = {2}", name, age, city); } Static voidMain (string[] args) { //Standard CallSearch ("Sue", A,"New York"); //Omit City parameterSearch ("Mark", at); //To explicitly name the city parameterSearch ("Lucy", City:"Cairo"); //use named parameters in reverse orderSearch ("Pedro", Age: $, City:"Saigon"); } }}
View Code
1.b.2,
1.C, (free Download) back to top |
|
Ylbtech Source: http://ylbtech.cnblogs.com/ This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. |
Ylbtech-languagesamples-namedandoptional (named and optional parameters)