Do you really know the values and references in C? (Lower)

Source: Internet
Author: User

Two days ago I discussed some common misunderstandings about the storage location of value type. I did not think that I think there are still some people who have doubts about the well-known secrets. Although I cannot cite strong evidence to prove this (reference type value type fields are stored on the stack), this is actually the implementation details. What I want to emphasize in my previous article is that we cannot regard implementation details as truth, because it is unstable.
 
The topic to be discussed today is parameter passing, which is not the implementation details.
 
Parameter type
Parameters in C # are divided into four types:
 
Value parameter (parameters passed by value)
Reference parameters (for parameters passed by reference, use the ref modifier)
Output Parameters (use the out modifier)
Parameter array (using the params modifier)
This article mainly discusses the differences between passing parameters by value and passing by reference, as well as the performance of passing values and reference types by value and by reference. The following uses passing parameters to a method as an example.
 
Parameters passed by value
C # parameters are passed by value by default. That is to say, when a parameter is passed to a method, a new storage location is created, and the parameter value is copied to the storage location. It is equivalent to declaring a local variable (real parameter), and then initializing the variable with the input parameter value. If the value of the real parameter is changed in the method, the context of the method call is not affected.
 
As mentioned in the previous article, the value of a value type expression is the data, and the value of a reference type expression is the reference of an object. Therefore, when passing by value, for a value-type parameter, the data stored by the value-type parameter is copied; for a parameter of the reference type, the copied value is a reference to a specific object. (Note: This is the same as assigning values to another variable directly .)
 
Value Type passed by value
Note: As mentioned above, when the value type is passed by value, the data represented by the value type itself will be copied. The following code snippet:
 
// # Code1
Int I = 5;
N (I );
Console. WriteLine (I );
...
Void N (int j)
{
J = 10;
} Although j is set to 10 Within N, in the context of method calling, the value of I is still 5, changing a copy of j. As shown in:
 
 
 
Reference Type passed by value
For parameters of the reference type, the copied value is a reference to a specific object. The following code snippet:
 
// # Code2
StringBuilder sb1 = new StringBuilder ("Hello ");
M (sb1 );
Console. WriteLine (sb1 );
...
Void M (StringBuilder sb2)
{
Sb2 = null;
} Although sb2 is set to null inside M, the actual output is still "Hello ". As shown in:
 
 
 
Note: we are talking about changing the value of the real parameter within the method without affecting the external call context. For real parameters of the reference type, if you change the data of the referenced object within the method, the real parameters and external variables still reference the same object, so they will be affected. For example, the following code:
 
// # Code3
StringBuilder sb1 = new StringBuilder ("Hello ");
M (sb1 );
Console. WriteLine (sb1 );
...
Void M (StringBuilder sb2)
{
Sb2.Append ("world ");
} The output is "Hello world ". As shown in:
 
 
 
Parameters passed by reference
Implicit replication is not involved in transferring data by reference. It is not the value of the variable passed to the method when calling the method, but the variable itself. Instead of creating a new storage location, it uses the same storage location as the variable. Therefore, the variable passed to the method in the call context is actually the same as the parameter used inside the method.
 
When passing parameters by reference, the ref modifier must be explicitly used in both method declaration and call, this is to make it clear that you are performing a reference-based transfer that is different from the default transfer method.
 
Value Type passed by reference
According to the definition passed by reference, we actually pass the variable itself to the method. In the method and the place where the method is called, the same variable is actually used. Therefore, any changes made to parameters within the method are also reflected outside the method. # After Code1 is changed to pass by reference, the output result is 10.
 
// # Code4
Int I = 5;
N (ref I );
Console. WriteLine (I );
...
Void N (ref int j)
{
J = 10;
} The parameter transfer process is shown in:
 
 
 
Reference Type passed by reference
The application type is the same as the application transfer and value transfer by reference. Any changes made within the method are reflected in the external variables. Therefore, for example, if # Code2 sets sb2 to null, the external sb1 will also become null. For example, if # Code3 calls sb2.Append, the external sb1 will also change accordingly.
 
// # Code5
StringBuilder sb1 = new StringBuilder ("Hello ");
M (ref sb1 );
Console. WriteLine (sb1 );
...
Void M (ref StringBuilder sb2)
{
Sb2 = null;
}:
 
 
 
Conclusion
We usually mention the values and references in C #. In most cases, they may refer to the value type and reference type, but in fact the values and references have a richer meaning. These two articles try to summarize these concepts and explain the values of the value type and reference type, what are the similarities and differences between them in value-based transmission and parameter-based transmission. More importantly, we often have many misunderstandings about the value type and reference type, and remember the implementation details as knowledge points. In fact, these are just their own, not their own. However, we do not need to go further.
 
Many interviewers will not ask themselves for implementation details when examining their interviewees. This is completely unnecessary. (Of course, the interviewer may also fall into such a misunderstanding .) In the future, we should keep in mind that when we talk about the difference between the reference type and the value type, if someone talks about the storage location (90% of people will probably do this ), you should direct them to the right direction.
 
References
Parameter passing in C #


Author: Qilin. NET

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.