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 call the C# method

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge about how to call the C# method. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

1. What is the method?

Method is a block of code that organizes related code statements together to perform a task.

So what is the function of the method?

One is to reduce redundant code and make the program look less bloated and cluttered.

Second, it is easy to read and maintain the code in the later stage.

For example: mining.

If there is no special method, then every time you dig a mine, you need to write all the code about mining. In this way, there will be a lot of code, and when you want to modify the code later, you have to change every location, which is extremely troublesome.

With a method, you only need to throw a series of mining operations into a method, and when you want to dig a mine, just call the method. The following calculation is to add, delete and modify the function of the method, and only need to operate on this method.

2. The composition of the method

Method composition format:

[static] (parameter type 1 parameter name 1, parameter type 2 parameter name 2) {method body}

Access modifier: determines whether the method can be visible to other classes. For example, public is visible to everyone, while private is visible only to itself.

Static: stands for static and is unique in a class. If the method is decorated with static, you do not need to instantiate the class object, but you can call the method name directly; if there is no static modification, you need to instantiate the class object to use.

Return type: determines the data type of the last return value of the method. Here, except that void does not need return, all other types need return to return a value.

Method name: that is, the name of the method. When the method is written and the method is to be called, it can be called directly through the method name.

Parameters: there can be multiple parameters, or they can be useless. Each parameter must have a corresponding data type and parameter name, which is called in the method.

Method body: that is, to implement the code statement written by a task.

3. Method invocation instance (1) does not use static modification: class C1 {static void Main (string [] args) {int N1 = 20; int N2 = 30; C1 C1 = new C1 (); int count = c1.addNum (N1, N2); Console.WriteLine ($"{N1} + {N2} = {count}") / / 20,30,50} public int addNum (int num1, int num2) {int num = num1 + num2; return num}} (2) modified with static: class C1 {static void Main (string [] args) {int x = 300; int y = 100; c2.swap (x, y); Console.WriteLine ($"after exchange, x is {x}, y is {y}") }} class c2 {public static void swap (int N1, int N2) {Console.WriteLine ("I really got called!") ; int temp = N1; N1 = N2; N2 = temp;}}

As you can see from the print result, the swap method in c2 has indeed been called, but the result has not changed.

This is because the method uses parameters passed by value, N1 and N2 in the method are formal parameters, and x and y are arguments when the method is called. Arguments and parameters are values in two different memory, and when a method is called, the value of the argument is assigned to the parameter. At this time, although the parameter has changed, it will not affect the value of the parameter.

If you want to get a result, change the return type void to int, with return returning two values at the end. When you call the method, you can assign values to x and y.

In addition, there are other ways to solve the problem.

4. Parameter transfer

There are three ways to pass parameters, namely: passing parameters by value, passing parameters by reference, and passing parameters by output. Passing parameters by value is already shown above, so there will be no more introduction.

(1) pass parameters by reference

A reference parameter is a reference to the memory location of a variable. When parameters are passed by reference, unlike value parameters, it does not re-create a new storage location for those parameters. The reference parameter has the same memory location as the actual parameter provided to the method.

In other words, if the parameter changes, so will the actual parameter.

Note: in C #, reference parameters need to be declared with the ref keyword.

Class C1 {static void Main (string [] args) {int x = 300; int y = 100; Console.WriteLine ($"before exchange, x is {x}, y is {y}"); c2.swap (ref x, ref y); Console.WriteLine ($"after exchange, x is {x}, y is {y}") }} class c2 {public static void swap (ref int N1, ref int N2) {int temp = N1; N1 = N2; N2 = temp;}}

From the result, the shape parameter has changed, the actual parameter has also changed.

(2) pass parameters according to output

The return statement can be used to return only one value from a function. However, you can use the output function to reverse two values from the function. The output parameter assigns the data output by the method to itself, and is similar to the reference parameter in other aspects.

Note: the output parameters need to be modified with the out keyword.

Class C1 {static void Main (string [] args) {C1 C1 = new C1 (); int a = 100; Console.WriteLine ($"before method is called, an is {a}"); c1.getValue (out a); Console.WriteLine ($"after method is called, an is {a}"); c1.getValue (out int b); Console.WriteLine ($"No assignment, b is {b}") } public void getValue (out int x) {int temp = 5; x = temp;}}

Like the reference parameter, the output parameter is the same memory address as the actual parameter. If the output parameter changes, the actual parameter will also change.

The difference is that the actual parameter of the reference parameter needs to be assigned, while the actual parameter of the output parameter only needs to declare the type of the variable, not the initial value of the variable, which automatically assigns the value in the method to the variable.

These are all the contents of the article "how to call the C# method". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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