Home > Backend Development > C++ > How Does C# Parameter Passing Really Work: Value vs. Reference?

How Does C# Parameter Passing Really Work: Value vs. Reference?

DDD
Release: 2025-01-31 00:36:10
Original
323 people have browsed it

How Does C# Parameter Passing Really Work: Value vs. Reference?

C# parameter transfer: the in -depth discussion of the value transmission and reference transfer

In C#, value transmission and reference transfer are the basic concepts that programmers must have. Although non -original types are generally considered to be transmitted, the situation is not always the case for certain non -original objects (such as System.Drawing.image).

System.drawing.image object's particularity

When passing the System.Drawing.image object to the method and loading images to it, we usually expect the original object to reflect these changes after returning. However, this is not the case for System.Drawing.image. The changes made in the call method will not spread the recovery recipient, which will cause confusion.

The truth of "reference transmission"

This misunderstanding comes from misunderstandings about the meaning of reference. In C#, the object itself will not be transmitted. Conversely, their values ​​are passed on the value of the initial parameter. For the type of reference, these values ​​are references to the object itself. Therefore, any changes made by the object itself in the call method will be visible to the call party. But what is important is to note that the value of the parameter itself (that is, the reference to the object) is not passed according to reference. This means that if the parameters are allocated a new value and quote a different object, the call party will not see this change.

Solution: Expressing reference transfer

In order to ensure that the calling part itself is visible, the call must be passed. Regardless of whether the parameter is the value type or the reference type, this can be implemented by

or modifiers.

By using these modifiers, the parameters are actually passed according to reference, and the changes it make in the method will be reflected in the calling party.

out Example ref

In order to explain this concept, please consider the following example:

In the first example, the parameter is passed by value, and the changes to the parameter itself are not visible to the call party. In the second example, the modifier formula is displayed according to the reference parameter, which is visible to the parameter itself. In the third example, the parameters are passed by reference, but the changes to the object itself are visible, because the parameter value is a reference to the object.

Conclusion

public void Foo(Image image)
{
    // 此更改对调用方不可见。
    image = Image.FromStream(...);
}

public void Foo(ref Image image)
{
    // 此更改对调用方可见。
    image = Image.FromStream(...);
}

public void Foo(Image image)
{
    // 此更改对调用方可见。
    image.RotateFlip(...);
}
Copy after login

C# reference transfer is a subtle concept, and its behavior is not always as expected by our intuition. Understanding the difference between the transmission object and the reference transmission value is essential for the code that runs as expected. ref

The above is the detailed content of How Does C# Parameter Passing Really Work: Value vs. Reference?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template