Get the App
SLTechnology News&Howtos  ›  Development  › 

How to apply named parameters and optional parameters in C #

Shulou Source: shulou.com Published: 2022-05-31 20:35:24 09月21日 Update

Most people do not understand the knowledge points of this article "how to apply named parameters and optional parameters in C#", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "how to apply named parameters and optional parameters in C#" article.

I. brief introduction

The parameters of the design method are that some and all parameters can be assigned default values, and then when calling these methods, you can choose not to provide some arguments and use the default values defined by the parameters, in addition, you can also pass the arguments by specifying the parameter name when calling the method.

Code case class Program {private static int s = 0; private static int swatches = 0; private static void Mothed (int x = 9, string s = "A", DateTime dt= default (DateTime), Guid guid= new Guid ()) {Console.WriteLine ("x = {0}, s = {1}, dt= {2}, guid= {3}", x, s, dt, guid) } static void Main (string [] args) {Mothed (); / equivalent to Mothed (9, "A", default (Datetime), new Guid ()) Mothed (8, "X"); / / equivalent to Mothed (8, "X", default (Datetime), new Guid ()) Mothed (5, guid: Guid.NewGuid (), dt: DateTime.Now) / equivalent to Mothed (5, "A", DateTime.Now,Guid.NewGuid ()) Mothed (sroomnails, s_n++.ToString ()); / / equivalent to Mothed (0, "1", default (Datetime), new Guid ()); Mothed (s: (scoundrel +) .ToString (), x: sprinter +) / / is equivalent to the following two lines of code. Note that the order of the parameters here is independent of the order of the parameters, corresponding to / / string T1 = "2"; / / int32 t2quo3; / / Mothed (t2dint T1) default (Datetime), new Guid ()}}

When you pass arguments to a method, the compiler evaluates the arguments from left to right. In the fourth call to Mothed, the current default value of sroomn is 0, which is incremented and then equals 1 to string to s, and then continues to be incremented to 2 (sroomn _ increment + first used and then incremented) to x.

The above is the content of this article on "how to apply named parameters and optional parameters in C#". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Parameters content method caching application code name article time knowledge article part sequence value most that is more cases steps knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Docker Shulou Information Microsoft