Asp.net (C #) function object parameter transfer

Source: Internet
Author: User

Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
TestClass objA = new TestClass ();
ObjA. Name = "I am ObjA ";

Console. WriteLine (String. Format ("In Main: {0}", objA. Name ));

TestFun (objA );

Console. WriteLine (String. Format ("In Main: {0}", objA. Name ));

Console. Read ();
}

Static void TestFun (TestClass obj)
{
Obj. Name = "I am be modified in TestFun ";
Console. WriteLine (String. Format ("In TestFun: {0}", obj. Name ));
}

Public class TestClass
{
Public string Name {get; set ;}
}
}

The object objA is passed as a parameter to the function TestFun. In this case, the reference of the object objA is passed. Therefore, the changes to the objA in the function will be directly applied to the object itself. The Name attribute of the parameter obj is modified in the function TestFun. Therefore, when objA. Name is called in Main, this attribute value is changed to the modified value in TestFun. This is easy to understand and the running result is:

If we use another method:
Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
TestClass objA = new TestClass ();
ObjA. Name = "I am ObjA ";

Console. WriteLine (String. Format ("In Main: {0}", objA. Name ));

TestFun (objA );

Console. WriteLine (String. Format ("In Main: {0}", objA. Name ));

Console. Read ();
}

Static void TestFun (TestClass obj)
{
TestClass objB = new TestClass ();
Obj = objB;
Obj. Name = "I am ObjB ";
Console. WriteLine (String. Format ("In TestFun: {0}", obj. Name ));
}

Public class TestClass
{
Public string Name {get; set ;}
}
}

What is the result? In TestFun, a new object objB is created, and obj is directed to the newly created object objB, and its Name attribute is assigned a value. Therefore, the object is stored in the Console in the TestFun function body. obj. the Name should be "I am ObjB ". So after TestFun (objA) is called in Main, has objA changed? Does it still point to the original object or has it pointed to the objB created in TestFun?

My guess is that the actual address passed by objA to TestFun is the objA address. The correspondence before TestFun is called:

ObjA is a variable whose own address is 000001. It stores the address 00E001 of the objA object. When TestFun (objA) is called, because objA needs to be passed to TestFun as a parameter
The stack copies a copy of The objA address. The address of this copy is 000003, And it stores the address of the objA object 00E001,

Therefore, in the TestFun function, obj points to the objA object at the beginning. After an object objB is created in TestFun, the memory model changes:

After "obj = objB" is executed in TestFun, obj points to the objB object, for example:

After the TestFun function is executed, the objA in the Main function still points to the original objA object, so the running result is as follows:

 

Note: The Memory Model in this article is only a personal guess. It must be incorrect or not rigorous. I hope it will not mislead you. please correct me.

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.