| |

VerySource

 Forgot password?
 Register
Search
Author: duck042432

string, value type reference type problem

[Copy link]

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-4 16:51:01
| Show all posts
芬恩克莱尔
-----------------------------------------
  You say "str =" string being converted. ";

Point str to something else, while str1 still points to "str".

"
 Here "string being converted."; Who was pointed to in memory? str is just a formal parameter, who is actually pointed at by that pointer? string being converted. "; Seems like there is no pointer, so I want to output string being converted."; (without ref)
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-4 17:03:01
| Show all posts
I want to change all str1 in the question to str. The answer is still the same ~~
So there are 2 pointers str with the same name on the stack? ? Scope issues ~ (global and local variable issues ??) ~
I have n’t had any major problems with string before. I have to think about it every time I use it.
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-1-5 08:27:01
| Show all posts
TO: static void StringConvert (string str)
 {
           str = "string being converted.";
      }
 static void Main ()
 {
 string str1 = "str";

 StringConvert (str1);
.....

Is such that:
string str1 = "str";
Executing this sentence allocates an address in memory, puts it into "str", and points to it by str1 ...

After calling StringConvert (str1) ;, str points to str1, which is the address where "str" ​​is located, but there is such a code in the method: str = "string being converted."; At this time, another space will be opened in memory "string being converted.", and it is pointed to by str, but at this time str1 still points to "str" ​​and has not changed ..

This is my understanding ... for reference only ...
Reply

Use magic Report

0

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-6 09:42:01
| Show all posts
See my blog, maybe it's useful for you to understand
http://blog.csdn.net/yonger/
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-6 10:33:01
| Show all posts
top
Reply

Use magic Report

0

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-1-7 00:54:01
| Show all posts
yujunziling
 
 
   
C # 's string is a bit special.It is actually a value type, but has the characteristics of some reference types, that is, it is wrapped with a layer of object characteristics outside of its essence.

  
=====================================
Except for this answer, you can see everything else.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Gabon

Post time: 2020-1-11 06:54:01
| Show all posts
Authoritative explanation:
This is not the particularity of string references, but the particularity of string operations themselves
The C # specification states that string objects are not modified once they are created.
If the content of a string reference is modified, the original reference unit is discarded and the unit containing the new value is referenced
This explains the particularity of the so-called string type reference and object type reference in the above case.
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-8-15 17:00:01
| Show all posts
Acknowledged, I checked it out and I was talking nonsense...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list