C # Object-Oriented Programming-out parameters of the warm and newest Learning Series (6)

Source: Internet
Author: User

  Preface

I. out parameters

Ii. Note:

  I. out parameters

The out keyword is similar to the ref keyword. The out keyword also leads to parameter passing through references. Unlike the ref keyword, the ref keyword requires that the variable be initialized before being passed, the out keyword does not require variables to be initialized before being passed, but must be assigned a value to the variable in the method. If the out keyword is used, the out keyword must be displayed for both the method definition and the method to be called.

  Ii. Note:

1: If the out parameter is not assigned a value in the method body, compilation will fail.

When will the out parameter be used?

When passing a parameter in a method, the out keyword is used to indicate that the variable must return a value. For example, a division method can get both the quotient and remainder, but a common method can only return one value, in this case, the out parameter can be used to return another value. Of course, in addition to this method, the returned value is an array or multiple values can be returned.

Instance

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace _ 6_out
{
Class Program
{
Static void Main (string [] args)
{
Int I = 15;
Int j = 6;
Int yushu;
Person person = new Person ();
Console. writeLine ("{0}/{1} = {2} -- {3}", I, j, person. getShangAndYu (I, j, out yushu), yushu );
Console. ReadKey ();
}

}
Class Person
{
Public int GetShangAndYu (int I, int j, out int yushu)
{
Yushu = I % j;
Return I/j;
}
}
}

Running Effect

  

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.