On the Out keyword in C #

Source: Internet
Author: User

As with the REF keyword, the Out keyword is also passed by reference.

The example shows how to use the Out keyword to get the index of the maximum and maximum values in the array

Using System;

Using System.Collections.Generic;

Using System.Text;

namespace Sampsong

{

class Program1

{

///<summary>

///The index of the maximum and maximum values of the array

///</summary>

///<param name= "M_array" > incoming array </param>

///<param name= "M_index" > maximum index required </param>

///<returns > maximum values in arrays </returns>

static int Maxindex (int[] M_array, out int m_index)

{

M_index = 0;

int m_max = m_array[0];

For (int i = 0; i < m_array.length; i++)

{

if (M_array[i] > M_max)

{

M_max = M_array[i];

M_index = i;

}

}

return M_max;

}

static void Main (string[] args)

{

int[] myarray=new int[5]{56,89,425,21,21};

int maxindex;

Console. WriteLine ("The largest value in the array is: {0}", Maxindex (MyArray, outmaxindex));

// The value of Maxindex after calling the Maxindex method is 2

Console. WriteLine ("The largest value in the array is the {0} element", maxindex+1);

}

}

}

The largest value in the array is: 425 the largest value in the array is the 3rd element, press any key to continue ...

Description

The 1.out keyword causes parameters to be passed by reference. This is similar to the ref keyword, except that the ref requires that the variable be initialized before it is passed.

2. both the method definition and the calling method must explicitly use the Out keyword.

3. The property is not a variable and therefore cannot be passed as an out parameter.

4. Example

Using System;

Using System.Collections.Generic;

Using System.Text;

namespace Sampsong

{

class Program1

{

static void Method (out string name, out int grad,out string school)

{

Name = " Song";

Grad = 2005;

School = " Zhengzhou University";

}

static void Main (string[] args)

{

String Name;

String School;

int Grad;

Method (outName, outGrad, outSchool);

             Console.WriteLine ( " call method" "

Console.WriteLine (" student" + Name + " yes" + School + Grad + " level student");

}

}

}

Run results

After calling method methods

On the Out keyword in C #

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.