C # keywords and ref and out

Source: Internet
Author: User

Recently, I encountered a ref and out parameter problem when writing a program. I looked back at MSDN to consolidate the foundation. I will post my test program to share with you.
The ref keyword allows the parameter to be passed by reference. The effect is that when the control is passed back to the call method, any changes to the parameters in the method will be reflected in this variable. To use the ref parameter, you must explicitly use the ref keyword for both method definition and call methods. Parameters passed to the ref parameter must be initialized first. This is different from out. The latter parameter does not need to be explicitly initialized before being passed. This is the out and ref.
It is a bit like passing references and passing values through functions in c ++. A typical program is to exchange two numbers. The code is not pasted here.

Using System;
Using System. Collections. Generic;

Public class MyClass
{
Public static void Main ()
{
 
/// The test shows that the value of a base-type parameter is not changed after the ref declaration is not used, indicating that the value passed in this function is.
/// However, when the ref parameter is used, it is found that the value has changed, indicating that reference is passed in this function.
Int x = 100;
ChangeTheNumber (ref x );
Console. WriteLine (x. ToString ());


/// Test shows that the attributes of a class instance are changed even if the ref parameter is not used. This is because the instance of the class is a reference. Therefore, you no longer need to use ref.
/// This reminds me of passing parameters between different forms during project development a few months ago, but I always wanted to create a common method and call each other. However, it is actually very simple,
/// Input the instance of the class to be passed in the constructor, because the reference is passed. Its attributes will change. There is no need to use any more troublesome methods.
/// TestTheClass
TestClass classInstance1 = new TestClass ("firstname ");
ChangeTheNumber (classInstance1 );
Console. WriteLine (classInstance1.Name );

TestClass classInstance2 = new TestClass ("second ");
ChangeTheNumber (classInstance2 );
Console. WriteLine (classInstance2.Name );
Console. Read ();
}
 
# Region Helper methods

Private static void WL (object text, params object [] args)
{
Console. WriteLine (text. ToString (), args );
}
 
Private static void RL ()
{
Console. ReadLine ();
}
 
Private static void Break ()
{
System. Diagnostics. Debugger. Break ();
}

Private static void ChangeTheNumber (ref int number)
{
Number = 100;
}
 
Private static void ChangeTheNumber (int x)
{
X = 100;
}
 
Private static void ChangeTheNumber (TestClass classInstance)
{
ClassInstance. Name = "change to another name ";
}
 
Private static void ChangeTheNumber (ref TestClass classInstance)
{
ClassInstance. Name = "change to another name ";
}
# Endregion
}

///
/// Test class
///
Public class TestClass
{
Public TestClass (string name)
{
This. name = name;
}
 
Private string name;
Public string Name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
}

Related Article

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.