如何在c#中实现out参数 [英] How to implement in out parameter in c#

查看:68
本文介绍了如何在c#中实现out参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在C#中实现out参数。



我知道如何在C#中使用out参数。但我想要一些我想在out参数中传递值的东西。该值将在我调用的函数中使用,然后我将更改函数中的参数值,该值必须反映回被调用者。



I希望ref可以用于此。是否有其他东西我可以用于此...?

How can we implement in out parameter in C#.

I know how to use an out parameter in C#. But I want something that I wanna pass a value in the out parameter . The value will be used in the function I called and then I will change the value of the parameter in the function which has to be reflected back to the callee.

I hope ref can be used for this. Was there is any other thing that I can use for this.. ?

推荐答案

通过ref传递是你的答案..





Pass by ref is your answer..


public static DataTable GetAddressData(string sprocName, string inputXML, ref string message)
      {
          //make a call to DB get your datatable
          message = "success";
          return dataTable;
      }







这样叫它






Calling it this way

addressData = AvData.GetAddressData(sprocGet, getXML, ref messge);


除了解决方案1.您还可以使用 out 参数向其传递初始值,即使这样参数传递的形式不适用于此类用途。 ref out 参数传递的 by-reference 机制。区别在于: out 参数是为输出而设计的,因此编译器在调用之前不会强制执行此类参数的初始化。另一方面,编译器会在返回之前强制将某些值赋值给 out 参数。



In与此相反, ref 参数必须在调用之前初始化。另一方面,方法实现不必为此参数指定任何内容。



基本上,in-out参数为 ref



-SA
In addition to Solution 1. You can also use out parameter to pass an initial value to it, even though this form of parameter passing is not intended for such use. Both ref and out the by-reference mechanism of parameter passing. The difference is: out parameters are designed for output, so the compiler won't enforce initialization of such parameter before the call. From the other hand, the compiler will enforce assignment of some value to the out parameter before return.

In contrast to that, ref parameter must be initialized before call. From the other hand, the method implementation does not have to assign anything to this parameter.

Essentially, "in-out" parameter is ref.

—SA


这篇关于如何在c#中实现out参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆