Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.. ?
Posted

Pass by ref is your answer..


C#
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

C#
addressData = AvData.GetAddressData(sprocGet, getXML, ref messge);
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900