Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the methods and parameters of CLR Via

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to analyze the methods and parameters of CLR Via, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Usually when we use a method, the parameters of the method are passed by value, and if the parameter passed is an object of reference type, the address of the reference object is passed to the method. If you pass an instance of a value type, you pass a copy of that instance to the method. CLR Via allows you to pass parameters by reference in a method, which is represented by the out and ref keywords in C #. Here's how to use out and ref.

1. Out of CLR Via

(1) when using out, you should use the out keyword when defining the parameters of the method and when calling the method, as follows:

Static void Main (string [] args) {string name = string.Empty; GetStr (out name); / / add out Console.WriteLine (name) when calling;} private static void GetStr (out string name) / / out {name = "oec2003";}

(2) if a method has parameters modified by out, you must assign a value to the parameter before the end of the method, otherwise it cannot be compiled. The code is as follows:

Static void Main (string [] args) {string name = "oec2003"; GetStr (out name); Console.WriteLine (name);} private static void GetStr (out string name) {/ / does not assign a value to name. There will be an exception that the out parameter "name" must be assigned before the control leaves the current method.

(3) when calling a method with an out parameter, it is not necessary to assign an initial value to the out parameter, because the assigned value will not be passed to the inside of the method. If you want to force the use of the out parameter inside the method, there will be compilation errors. Look at the code below, usually we can use the out parameter when we need to return multiple values in a method. Like the out parameter, the ref keyword is used when using ref in both the parameter definition of the method and the call to the method. Unlike out, the value of the ref parameter can be passed into the method for operation.

Static void Main (string [] args) {string name = "oec2003"; / / assign the initial value oec2003 GetStr (out name) to the out parameter name; Console.WriteLine (name);} private static void GetStr (out string name) {name = "hello" + name / / name is assigned to oec2003 before the call, / / but an error is reported here that "unassigned out parameter name" is used.

(4) if the number and type of parameters of the two methods are the same, the only difference is that one of them is an out parameter, then the two methods can be overloaded, and the following code can run normally.

2. Ref of CLR Via

Private static void GetStr (out string name) {name = "oec2003";} private static void GetStr (string name) {name = "oec2003";}

(1) if the ref parameter does not have an initial value and cannot be compiled before calling the method, see the following code:

Static void Main (string [] args) {string name; GetStr (ref name); / / if name is not assigned, it cannot compile Console.WriteLine (name);} private static void GetStr (ref string name) {name = "oec2003" } static void Main (string [] args) {string name= "oec2003"; GetStr (ref name); Console.WriteLine (name); / / return: hello oc2003} private static void GetStr (ref string name) {name= "hello" + name;}

(2) because ref will have an initial value when passing in the method, there can be no operation on the ref parameter inside the method, so the value of the ref parameter will not change.

Static void Main (string [] args) {string name= "oec2003"; GetStr (ref name); Console.WriteLine (name); / / has no operation in the method and still returns oec2003} private static void GetStr (ref string name) {. }

(3) like the out parameter, if the number and type of parameters of the two methods are the same, the only difference is that one of the parameters is the ref parameter, and the two can be overloaded.

For CLR Via, the keyword out and keyword ref are equivalent, that is, the same IL code will be generated regardless of whether you use out or ref. Because of this, if the difference between the two methods is only the difference between out and ref, then the two methods cannot be overloaded, as shown in the following code:

/ / the following code compilation will report that "overloaded methods that are different only on ref and out cannot be defined" exception private static void GetStr (ref string name) {name = "oec2003";} private static void GetStr (out string name) {name = "oec2003";}

3. Variable number of parameters of CLR Via

Sometimes if the number of parameters of a method can be changed according to the needs of users, it will bring great convenience. Methods such as Concat and Format of type String provide variable parameters. Variable parameters can be defined using params in C #, as shown in the following code:

Static void Main (string [] args) {Console.WriteLine (Add);} public static int Add (params int [] num) {int sum = 0; foreach (int i in num) {sum + = I;} return sum;}

Using variable parameters is very simple, and it is important to note that the type of variable parameters must be an array type. Variable parameters are easy to use, but methods that accept variable parameters cause some performance loss when called, assuming that array objects must be allocated on the heap and that array memory eventually needs to be reclaimed by GC. In order to avoid this performance loss, we can define more overloads of methods without the params keyword when writing methods, so that methods with the params keyword will only be used in very special cases.

4. Parameter types of CLR Via method

When declaring method parameter types, you should use only weak types as far as possible. For example, if you want to write a method that manipulates a set of data items, * use interfaces (such as Ienumerable) to define the types of method parameters, instead of using strong data types such as List or some strong interface types (such as Ilist or Icollection), as follows:

/ / weakly typed parameter private void OperateCollection (IEnumerable collection) {} / / strongly typed parameter private void OperateCollection (List collection) {. }

The strong type and weak type referred to here can be understood as the level of the type. If the level of the parent class is higher than that of the child class, then the higher the level, the weaker the type. The Iemumerable interface is directly under the System.Collections namespace and is the base class of some other collection classes and interfaces (such as Icollection IList List, etc.), so if the parameter is defined as IEnumerable, any parameter that inherits IEnumerable can be passed into the method, which greatly improves the flexibility.

After reading the above, have you mastered how to analyze the methods and parameters of CLR Via? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report