Dot Net Perlsc# optionalTop 37 C# Example Pages

["(wrye.XV(ST~~}T~~YF66666554F(B.CEP)CXS}T~~}T~~","Optional Parameters."," An optional parameter has a default value. A method with an optional parameter can be called with only some of its parameters specified. Using this feature in new versions of the C# language, we add default values for formal parameters.","Example."," In this example, we introduce a method named \"Method\" that has two parameters. Each of the parameters is optional. To specify an optional parameter, assign the formal parameter in the method parameter list to an appropriate value.","Here,"," we set the formal parameter 'value' to 1, and the formal parameter 'name' to \"Perl\". Whenever Method is called without a parameter specified, its default value is used instead in the method body. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Based on:"," .NET 4.6\n\n","C# program that uses optional parameters","\n\nusing System;\n\nclass Program\n{\n static void Main()\n {","\n // Omit the optional parameters.\n ","Method();","\n\n // Omit second optional parameter.\n ","Method(4);","\n\n // You can't omit the first but keep the second.\n // Method(\"Dot\");\n\n // Classic calling syntax.\n ","Method(4, \"Dot\");","\n\n // Specify one named parameter.\n ","Method(name: \"Sam\");","\n\n // Specify both named parameters.\n ","Method(value: 5, name: \"Allen\");\n }\n\n static void Method(","int value = 1",", ","string name = \"Perl\"",")\n {\n Console.WriteLine(","\"value = {0}, name = {1}\"",", value, name);\n }\n}\n\n","Output","\n\nvalue = 1, name = Perl\nvalue = 4, name = Perl\nvalue = 4, name = Dot\nvalue = 1, name = Sam\nvalue = 5, name = Allen","This example shows"," that you can call Method with no parameters. You can call it with only an int parameter. You can use the regular calling syntax from previous versions of the C# language. And you can use the named parameter feature. ","Named Parameters ","named-parameters","Internals."," If you are interested in programming languages, you will want to know some information about how various features are implemented. For the optional parameter feature, an attribute is used to decorate the optional parameters. ","And: ","The attribute stores some object data about the default value of the parameter. You can learn more about attributes on this site.","Attribute ","attribute","Intermediate language for optional parameter method: IL","\n\n.method private hidebysig static void Method([opt] int32 'value',\n [opt] string name) cil managed\n{\n .param [1] = int32(1)\n .param [2] = string('Perl')\n .maxstack 8\n L_0000: ldstr \"value = {0}, name = {1}\"\n L_0005: ldarg.0\n L_0006: box int32\n L_000b: ldarg.1\n L_000c: call void [mscorlib]System.Console::WriteLine(string, object, object)\n L_0011: ret\n}","Performance."," What performance effect do optional parameters have? After benchmarking optional parameters, I found no performance effect. The C# compiler essentially transforms the calls where parameters were omitted to regular syntax calls. ","So: ","Method calls that omit parameters are the same speed as those that use the standard syntax.","Summary."," The optional parameter feature enables a more concise way of specifying methods that do not need all of their parameters specified. By using an assignment statement within the method parameter list, we specify an optional parameter. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANIAAABFCAMAAAAIJEiLAAAAGFBMVEUAru+g2Pf///8xt/BpxvPS6/vp9f0TtPCISbH1AAADCklEQVR4Xu2bwY7CMAxEPWM7/P8fr9baaNSG0lIQK6q8U2vhxk9ODD1gJBE3uwS3AEkjwy5EkCajqzgZ7GLAwi5G2M2uxceEJpPJZDKZTCYTzzCBsH8kAMCeA0OOJykNkLDP4iZAMu05UjkyYnMZveyEX3QJf9iVJN6npIC6FNTtWfSI/aeRfLsSpCAnup0nyF4nPqwkAwwV5ctKrS9G4qNKPtT/+nEK9t44/0EJ2mUiFTyr1EoEdSGlQGYi+t0vpRSFS6k+CZgwh5K1FJCJRkrJ7zYk7kQdiCeUUIskCSlFY8EWmiJCpy89WaRLiEruoLEjJfRNLxTesxRjdpB0r4ueB4p4rJTrQj0poHXuKG0UGsOcapXnh5WcJEDSajnNQWQJ+LaSUBG5Dsooc6EUW3W2laqziMNKlqVElFJ/ZLoavjkeCrhBn0OPRbm11VdeSknjZXfnPa0EsjUyuhLU5bp8rFQRnfqm5HIKXayVsmeLjfZBzT2mFCyslLSUtv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