How to get the memory address of a string in C#? -


could show me way memory address of string in c#? example, in:

string = "qwer"; 

i need memory address of a.

you need fix string in memory using fixed keyword , reference memory address using char*

using system;  class program {     static void main()     {         console.writeline(transform());         console.writeline(transform());         console.writeline(transform());     }      unsafe static string transform()     {         // random string.         string value = system.io.path.getrandomfilename();          // use fixed statement on char pointer.         // ... pointer points memory won't moved!         fixed (char* pointer = value)         {             // add 1 each of characters.             (int = 0; pointer[i] != '\0'; ++i)             {                 pointer[i]++;             }             // return mutated string.             return new string(pointer);         }     } } 

output

**61c4eu6h/zt1

ctqqu62e/r2v

gb{kvhn6/xwq**

share|improve answer

lets take situation surprised about:

string = "abc"; string b = a; = "def"; console.writeline(b); //"abc" why? 

a , b references strings. actual strings involved "abc" , "def".

string = "abc"; string b = a; 

both a , b references same string "abc".

a = "def"; 

now a reference new string "def", we've done nothing change b, still referencing "abc".

console.writeline(b); // "abc" 

if did same ints, should not suprised:

int = 123; int b = a; = 456; console.writeline(b); //123 

comparing references

now understand a , b references, can compare references using object.referenceequals

object.referenceequals(a, b) //true if reference same exact string in memory 
share|improve answer

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments