C #: PASS Parameters to methods

Source: Internet
Author: User

1.When a parameter is passed to a method through an applicationThe called method obtains this variable, so any changes made to the variable within the method still play a role after the method exits.

2.When a parameter is passed to a method through a valueThe called method gets a copy of the variable, that is, after the method exits, modifications to the variable will be lost.

Note: strings are different, because strings cannot be changed (if the value of a string is changed, a new string will be created), so strings cannot adopt the common reference type. In method calls, any changes made to the string will not affect the original string.

3.Ref Parameter: If you pass a parameter to a method with the ref keyword before the input parameter of this method, any change made to the variable of this method will affect the value of the original object.

4.Out keyword: The out keyword is used for initialization. When the out keyword is added before the input parameter of the method, the variables passed to the method can not be initialized. This variable is passed through reference.

Ref indicates inbound and outbound, and out indicates outbound.

Using system;

Namespace consoleapplication3
{
Class Program
{
Static void somefunction (Out int I)
{
I = 100; // note how I is declared but not initialized
}
Static void main (string [] ARGs)
{
Int I;
Somefunction (Out I );
Console. writeline (I );
}
}
}

5.Method Overloading: The method name is the same, but the number and type of parameters are different.

For method overloading, if an incorrect method is called, a running error may occur:

  • The two methods cannot be different only in the return type.
  • The two methods cannot be distinguished only based on whether the parameter is declared as ref or out.

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.