C # parameter transfer

Source: Internet
Author: User

Preface

The SQL statement repeated by cainiao has not yet been answered satisfactorily. Thank you very much if the eldest brother has relevant information to explain and can share it with me.

I have been in contact with C # for a year, and I feel a lot of things are still vague, like the delegation and events in C #.

Some things are too much to be used. Some other things are not refined even if you don't want them.

This time I found a detailed article about how to lift the confusions of parameter transfer in C #, so I couldn't help translate it for future review.

If the English version is good, click here to read the original text. Here is the link to the MSDN explanation.

Prelude: Reference Type

There are two common types in C #: And. They have different performances, and many people are confused when using them. Here we will explain their differences:

Reference type refers

StringBuilder sb =  StringBuilder();

Here we define a variable sb, create a new StringBuilder object, and assign this object reference to sb. The actual stored value of sb is not the object, but its reference.

The value assignment is simply to assign the expression or variable to the corresponding variable. Let's look at the following code:

StringBuilder first = =

Here we define a variable first, create a new StringBuilder object, and assign the reference of this object to first. Then we assign the first parameter to second.

This means they all point to the same object. If we use the first. Append method to modify the content of this object, second also changes. As follows:

StringBuilder first = =

Even so, they should still be seen as independent variables. Modifying first to point to a completely different object (or directly assigning null to it) does not affect second.

StringBuilder first = ==  StringBuilder(Console.WriteLine(second); 

Prelude: Value Type

There is an indirect layer between the variable and the real data, but it is not. Value Type variables directly store their data.

The value assignment is to copy the value and assign it to the corresponding variable. The following is an explanation of the structure type:

  

IntHolder is a struct that contains a separate integer I. The assigned values are copied as follows:

IntHolder first = = == 

Here the value of second. I is 5 because when second is set to first, second. I saves the copy of first. I. Since then, second is independent of first.

So even if first. I = 6, second. I remains unchanged.

 

: Many types (such as string) are, but they are like. These are the types of instances that cannot be changed once created.

When the object is referenced,

 

Use a variable reference type comparison (for example, ArrayList) to deepen your understanding.

If the return value of a method is an ArrayList reference in the variable, no new instance is created in the method for direct use,

But to add an existing instance and other operations, but other personnel do not know, this may cause problems.

As we have said before, you must never be confused by the surface, understand the essence, and use it better.

Test your understanding

If the previous IntHolder is not a structure type but a class, what is the output result? If you do not understand why it is 6, it is probably because I have a translation problem,

Or I am not clear enough to translate. I am very grateful if you have any suggestions.

Here we will provide a link to the original article, so that someone can't really see my bad translation.

Inter-play: Different Types of parameters

There are four different types of parameters in C :,,,.

You can use both value type and reference type parameters.

When using these parameters, you should have a clear understanding of "value type" and "reference type" in your mind.

In this way, you will feel very relaxed whether you are using them or the types related to them.

Value Type parameter

In C #, the default parameter is, which means that a copy of the variable will be created when the method member declares it. It is the initial value of the variable you specified to call within the method.

If you change this value, it will not affect the value source in the call. Check the Code:

= = ==);

The value of y is not changed because x is assigned null. In any case, remember that the reference type variable stores a reference -- if two references point to the same object,

Then, the content of the object will be changed as well. For example:

= 

After Foo (y) is called, the value pointed to by y is changed to "hello world". In the Foo method, Append is called to concatenate the "world" character on the referenced variable x to achieve the effect.

Here, let's take a look at how to pass. As mentioned earlier, a value of the value type is itself.

Using the previous structure type IntHolder, We will write some similar code to test it:

== =

When Foo is called, x is a struct type and its I is 5. Then 10 is assigned to I.

Foo does not know y at all. When the execution of this method ends, y is still the same as before.

We have previously demonstrated some examples about the reference type as the transfer.

Then you should understand what will happen when IntHolder declares it as a class. You should know why y. I is changed to 10.

Reference Type Parameters

When used, the actual value is not passed, but the variable itself is used. That is to say, the same storage address is used instead of creating a new copy.

Therefore, the value type and reference type in the method member are always the same.

You need to use the ref keyword when using the Declaration and call -- this means that your use will look clear and clear.

Let's change the previous example and test it:

 Foo (= = ==); 

Here, because the reference of y is passed to x rather than its value, the operation on x is equivalent to the operation on y. In the preceding example, y is null at last.

Compare the result with the example result without the ref keyword above.

Now, let's test what will happen after adding the ref keyword to the example with the structure as the parameter:

 Foo (== =

These two variables share a memory address, so y also changes when x is changed. So y. I is 10.

Note: What is the difference between transferring a reference type variable using the default value type parameter method and passing a value type variable using the reference type parameter method?

You may have noticed that in the last example, passing a structure type in reference mode has the same result as passing a class in value type mode.

But that doesn't mean they are the same. Okay, let's further understand through the following code:

 Foo (???= = =??? y);

Assume that IntHolder is a schema type (value type), and the method parameter is of reference type (coming soon ??? Replace with ref ),

Run the code, and y will finally point to a new IntHolder, and y. I will change to 0.

Assume that IntHolder is a class (that is, a reference type), and the method parameter is of the value type (coming soon ??? Remove ),

Run the code. The point of y is not changed, and y. I is still 5.

Before calling a function, y and x point to the same object. Understanding the difference in C # parameter passing is absolutely critical.

This is why the original author thinks that it is very confusing when people talk about the default reference transfer of objects. In fact, the correct statement should be the default value transfer of the reference type.

Out type parameter

It does not create a new memory address, but a shared memory address.

The keyword Out must be added when the used method is declared and called. This will also make the Code look clear.

Although very similar, they are still different. The Out type differs from the reference type:

1. You do not need to assign values to the variables passed during method calls. If the method call ends normally, you can think that the variable is assigned a value (in this way, you can directly read its value ).

2. This parameter is passed as not initialized (that is, you must assign a value to it before reading it ).

3. assign a value to this parameter before the end of the method. Otherwise, the compiler reports an error.

The following example demonstrates how to use the int type as a parameter.

Int Is a value type. If you have understood the reference type, you will also know what will happen when the reference type is used as a parameter:

 Foo (     x =      a =Foo (Console.WriteLine (y); 
Parameter array (params)

A group of data can be passed to a method. When defining the included method, you must add the keyword params, but you do not need to add this keyword when calling this method.

It must be placed at the end of the method parameter and can only be a one-dimensional array.

When this type of method is used, the parameter can be passed as long as the parameter is compatible with the parameter array type during definition.

Because of this usage, when you want to pass a separate array, the effect is like passing a value type parameter. For example:

 ShowNumbers (  ( x + [] x = {, , ,    

In the first call, x is an integer array, and the effect is equivalent to passing it as a reference.

During the second call, an integer array containing is created and Its Reference is passed (still yes ).

Final play: Summary

The translation is always easy to read and understand. I certainly cannot avoid translating technical documents for the first time. I only want to help you and me.

For programmers, English is very important.

If your English is better, I suggest you read the original article.

If any omission or misunderstanding is found in this article, please note that it is appreciated.

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.