Example analysis of out reserved word usage in C

  • 2020-11-03 22:33:38
  • OfStack

This article analyzes the usage of out reserved word in C# and shares it with you for your reference. Specific usage analysis is as follows:

The out reserved word in C# indicates that this variable returns a value. The simplest application is division. For example, you need 1 division method to get both remainder and quotient, but the normal method can only get 1 return value.

It is useful to declare the out method when you want it to return multiple values.

Methods that use the out parameter can still return 1 value. A method can have more than one out parameter.
To use the out parameter, you must explicitly pass the parameter to the method as an out parameter. The value of the out parameter is not passed to the out parameter.
It does not have to be initialized as a variable passed as an out parameter. However, the out parameter must be assigned before the method returns.
Attributes are not variables and cannot be passed as out parameters.

Examples are as follows:


// Get the return value 
private string sendMsg(string SendMessageResult, out int FailCount, out int SessCount, out int AllCount,out string sRe)
{
  string sStr = "";
  string Result = "";
  int dtCount = dtTemp.Rows.Count;
  int dtFailCount = dtCount;
  int dtSessCount = 0;
  sStr = SendMessageResult.Substring(0, 1);
  if (sStr == "0")
  {
 dtFailCount = Convert.ToInt32(SendMessageResult.Substring(2));
 dtSessCount = dtCount - dtFailCount;
 Result = " Send complete, this time send successfully " + dtSessCount.ToString() + " Article, failure " + dtFailCount.ToString() + " article ";
  }
  FailCount = dtFailCount;
  SessCount = dtSessCount;
  AllCount = dtCount;
  sRe = sStr;
  return Result;
}

// call 

private void btnSend_Click(object sender, EventArgs e)
{
int FailCount;
      int SessCount ;
      int AllCount;
      string sRe;
      string sSendMsg = sendMsg(e.Result, out FailCount, out SessCount, out AllCount, out sRe);
}

Hopefully this article has helped you with your C# programming.


Related articles: