Get the App
SLTechnology News&Howtos  ›  Development  › 

How to transfer parameters according to output in C #

Shulou Source: shulou.com Published: 2022-06-01 02:05:49 10月27日 Update

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!

Tags: Parameters outputs methods inputs caches functions facets variables learning code content examples local results compilation similarity two depending on that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Shulou Information OPPO Reno MariaDB