Method in 06. C # (2)

Source: Internet
Author: User


1.Parameter Value

Namespace _ 06. Method _ 2 _
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

 

Private void Form1_Load (object sender, EventArgs e)
{
Int I = 0;
MessageBox. Show ("I =" + I );
}
Public void Method (int I) // public static void ValueMethod (int I)
{
I ++;
}
}
}

2.Reference parameters

Namespace _ 06. Method _ 2 _
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

 

Private void Form1_Load (object sender, EventArgs e)
{
Int I = 0;
Method (I );
MessageBox. Show ("I =" + I );
Int j = 0;
ReferenceMethod (ref j );
MessageBox. Show ("j =" + j );
}
Public void Method (int I) // public static void ValueMethod (int I)
{
I ++;
}
Public static void ReferenceMethod (ref int I)
{
I ++;
}
}
}

3.Output Parameters

Namespace _ 06. Method _ 2 _
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

 

Private void Form1_Load (object sender, EventArgs e)
{
Int I = 0;
Method (I );
MessageBox. Show ("I =" + I );
Int j = 0;
ReferenceMethod (ref j );
MessageBox. Show ("j =" + j );
Int k = 0;
OutputMethod (out k );
MessageBox. Show ("k =" + k );
}
Public void Method (int I) // public static void ValueMethod (int I)
{
I ++;
}
Public static void ReferenceMethod (ref int I)
{

// I = 0;

I ++;
}
Public static void OutputMethod (out int I)
{
I = 0;
I ++;
}
}
}

 

 

4.Passing variable parameters to methods

Namespace _ 06. Method _ 2 _
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

 

Private void Form1_Load (object sender, EventArgs e)
{
MessageBox. Show (addi (1, 2, 3) + "");
}
Static int addi (params int [] values)
{
Int sum = 0;
Foreach (int I in values)
Sum + = I;
Return sum;
}
}
}

5.Using arrays as method parameters (reference type)

Namespace _ 06. Method _ 2 _
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

 

Private void Form1_Load (object sender, EventArgs e)
{
Int [] arr ={ 100,200,300,400 };
PrintArr (arr );
Foreach (int I in arr)
MessageBox. Show (I + "");

}
Static void PrintArr (int [] arr)
{
For (int I = 0; I <arr. Length; I ++)
Arr [I] = I;
}
}
}

 

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.