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 transfer parameters according to output in C #

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

Share

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

This article mainly explains "how to transfer parameters by output". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "how to transfer parameters by output" together.

Pass parameters by output

The return statement can be used to return only one value from a function. However, you can use output parameters to return 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.

The following example demonstrates this:

Using System

Namespace CalculatorApplication

{

Class NumberManipulator

{

Public void getValue (out int x)

{

Int temp = 5

X = temp

}

Static void Main (string [] args)

{

NumberManipulator n = new NumberManipulator ()

/ * definition of local variables * /

Int a = 100

Console.WriteLine ("value of a before method call: {0}", a)

/ * call the function to get the value * /

N.getValue (out a)

Console.WriteLine ("value of an after method call: {0}", a)

Console.ReadLine ()

}

}

}

When the above code is compiled and executed, it produces the following results:

Before the method call, the value of a: 100, after the method call, the value of a: 5

Variables supplied to output parameters do not need to be assigned. Output parameters are especially useful when you need to return a value from a method where a parameter does not specify an initial value. Take a look at the following example to understand this:

Using System

Namespace CalculatorApplication

{

Class NumberManipulator

{

Public void getValues (out int x, out int y)

{

Console.WriteLine ("Please enter first value:")

X = Convert.ToInt32 (Console.ReadLine ())

Console.WriteLine ("Please enter second value:")

Y = Convert.ToInt32 (Console.ReadLine ())

}

Static void Main (string [] args)

{

NumberManipulator n = new NumberManipulator ()

/ * definition of local variables * /

Int a, b

/ * call the function to get the value * /

N.getValues (out a, out b)

Console.WriteLine ("value of an after method call: {0}", a)

Console.WriteLine ("value of b after method call: {0}", b)

Console.ReadLine ()

}

}

}

When the above code is compiled and executed, it produces the following results (depending on user input):

Please enter the first value: 7 Please enter the second value: 8 after the method call, the value of a: 7 after the method call, the value of b: 8 Thank you for your reading, the above is the content of "how to transfer parameters by output". After the study of this article, I believe you have a deeper understanding of how to transfer parameters by output in C#, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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