asp.net (C #) function Object parameter transfer problem _ practical Tips

Source: Internet
Author: User
Copy Code code 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 is 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, and the reference to the object Obja is passed, so the modification of the Obja within the function is applied directly to the object itself. The Name property of the parameter obj is modified within the function Testfun, so the property value becomes the modified value in Testfun when the obja.name is called in Main. This is well understood and the results of the operation are:

If we were to change the wording:
Copy Code code 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;}
}
}

And what about the results? A new object OBJB is created in Testfun, then obj points to the newly created object OBJB and assigns a value to its Name property, so Console.WriteLine should be "I am Obj.name" in the Testfun function body OBJB. After calling Testfun (Obja) in Main, did the Obja object change, does it still point to the original object or has it pointed to the OBJB created in Testfun?

My guess is that the obja passed to Testfun is actually Obja's address, and the corresponding relationship before the call to Testfun is:

Obja is a variable, his own address is 000001, it holds the Obja object's address 00E001. When the function Testfun (OBJA) is invoked, the Obja is passed as a parameter to Testfun, so the
A copy of the Obja address is copied from the stack, and the address of this copy is 000003, and he still holds the address 00E001 of the Obja object, as shown in figure:

So in the Testfun function, the first obj point to is the Obja object. When an object OBJB is created in Testfun, the memory model becomes:

After executing "OBJ=OBJB" in Testfun, obj points to the Objb object, as shown in the following figure:

So after performing the Testfun function, Obja still points to the original Obja object in the main function, so the results are as follows:

Note : The memory model in this article is just a guess, there must be incorrect or not rigorous place, hope not to mislead everyone, also welcome correction.

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.