String type analysis Overview:
String is the most useful type in C. However, its usual usage (ex, +, =, = etc .), it is easy to give people the illusion that it and other value types (valuetype, ex, Int, float, double etc .) yes! However, my --《Javascript Analysis 1 -- typeThis section analyzes the string type in Js. However, in the C # environment, what type of string is used (value type, reference type ?) What is there? First look at a demoCode:
// Description: verifies the string type through a simple code test.
//Copyright:Http://www.cnblogs.com/yangmingming
// Notes: The environment is a label-lbeldisplaystringtype with an empty initial value;
// A textbox-txtstringtype that shows the string type. It is initially empty;
// One submit button (with event) btnsubmit;
Public Partial Class Typetest: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
}
Protected Void Btnsubmit_click ( Object Sender, eventargs E)
{
String Strtesttype = " This is test string " ;
Lbeldisplaystringtype. Text = " Display the type of string " ;
If ( ! Strtesttype. GetType (). isvaluetype)
Txtstringtype. Text = " String is reference type " ;
Else
Txtstringtype. Text = " String is valuetype " ;
}
During the test, we can see the comparison between the initial interface and the interface after the button is triggered, as shown below:
As shown in Textbox, string is of the reference type! But why is it such a value type?
In fact, the string type is so special that the C # language takes special care of it, because it is largely different from the other object-reference type, therefore, C # has encapsulated it and made it have many features similar to the value type (this is evident from the heavy loading of the string object method in C ++ I learned previously !) In essence, it is a reference type. Therefore, we can use the special String object to extend to our next topic --FAQs about parameter transfer.
FAQs about parameter transfer:
According to anytao's "what you must know. net, the artistic influence of parameter transmission. I want to summarize the parameter transmission methods to consolidate what I have learned and clarify this issue. Parameter transfer is dividedPass by valueAndPass by reference(Ref passed by reference is provided in C #. The following two types are available --Value TypeAndReference Type, And discussed separately:
I:Passing parameters by value
With the help of the parameter passing method of the function and the string type analysis discussed above, we select the int type and string type reference type to test the different performance of the parameter passing by value, analyze the cause.
The background code is as follows:
// Description: verifies the value type and reference type through code testing, and shows different performance during the value transfer process.
//Copyright:Http://www.cnblogs.com/yangmingming
// Notes: The environment is the four textbox with an empty initial value. The preceding lbel label indicates its function.
// The button indicates that the function is called, and the comparison result is displayed through the textbox value.
Public Partial Class Paradelivery: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
}
Protected Void Btnsubmit_click ( Object Sender, eventargs E)
{
Int I = 1 ;
String Str = " This is a string " ;
Txtitivalue. Text = I. tostring ();
Txtrefvalue. Text = STR;
Addvaluetype (I );
Addreftype (STR );
Txtchangedintvalue. Text=I. tostring ();
Txtchangedrefvalue. Text=STR;
}
// Two called Functions
Private Void Addvaluetype ( Int Funi)
{
Funi = 2 ;
}
Private Void Addreftype ( String Funstr)
{
Funstr = " This is a changed string " ;
}
}
After the button is triggered, let's take a look at the comparison:
It can be seen that in our call function, the attempt to re-assign values to the value type variable I and the reference type variable STR is not achieved!
Analysis:
1. The result of passing by value of the value type is not changed. I believe there is no problem because it is only a copy of the original variable and does not affect the original value;
2. For the pass-by-value of the reference type, many people will take it for granted that such a re-assignment will change the original value of the string variable, but the result shows that everything has not changed! Although it is a value-based transfer of the reference type, it is essentially a reference (in this example, it is a reference to the STR variable ), pass the value type to its original copy-reference copy. When the copy value is re-assigned, only the value of the referenced variable is copied (The New String object address), without affecting the original string object and its corresponding reference. In this way, the result that the string object has not changed is displayed.
Ii. Passing parameters by reference
Using a display method similar to the above, we reveal how the value type and reference type behave when passed by reference? First look at the background code:
// Description: verifies the value type and reference type through code testing, and shows different performance during the value transfer process.
//Copyright:Http://www.cnblogs.com/yangmingming
// Notes: The environment is the four textbox with an empty initial value. The preceding lbel label indicates its function.
// The button indicates that the function is called, and the comparison result is displayed through the textbox value.
Public Partial Class Paradelivery: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
}
Protected Void Btnsubmit_click ( Object Sender, eventargs E)
{
Int I = 1 ;
String Str = " This is a string " ;
Txtitivalue. Text = I. tostring ();
Txtrefvalue. Text = STR;
Addvaluetype (RefI );
Addreftype (RefStr );
Txtchangedintvalue. Text=I. tostring ();
Txtchangedrefvalue. Text=STR;
}
// Two called Functions
Private Void Addvaluetype ( Ref Int Funi)
{
Funi = 2 ;
}
Private Void Addreftype ( Ref String Funstr)
{
Funstr = " This is a changed string " ;
}
}
We can see that we have added a reference transfer method for it. After the button is triggered, we can see that the result is:
It can be seen that by referencing and passing the call function, we have implemented changes to both the value type and the reference type. The reasons are analyzed below:
Analysis:
1. The result of passing values by reference is changed because when passing values by referenceParameter addressWhen changing a variable in the function body, the operation is to direct the address to the variable (similar to the re-assignment of the pointer to the variable in C, ex, * P = 3 );
2. Pass the reference type by reference. Since the address of this parameter is automatically passed when it is passed by reference, here is the address referenced by the string object (similar to the pointer concept in C, ex, ** P = 3). In this way, when the variable changes in the function body, the object itself is actually changed, thus implementing effective changes to the variable.
Summary: there are great similarities between the value type and the reference type in the process of passing by value and by reference, I think these are essentially required by passing by value and passing by reference. The difference is that the two types of value type and reference type are different, but their expressions are indeed the same. (The self-perception analysis is not very clear. It is better to use the managed heap diagram)