Click here to Skip to main content
15,898,943 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generating a unique license key Pin
Colin Angus Mackay12-Jan-05 23:21
Colin Angus Mackay12-Jan-05 23:21 
GeneralCalling a procedure on a loaded form Pin
StephenMcAllister12-Jan-05 22:53
StephenMcAllister12-Jan-05 22:53 
GeneralRe: Calling a procedure on a loaded form Pin
Stefan Troschuetz12-Jan-05 23:13
Stefan Troschuetz12-Jan-05 23:13 
GeneralRe: Calling a procedure on a loaded form Pin
Colin Angus Mackay12-Jan-05 23:16
Colin Angus Mackay12-Jan-05 23:16 
GeneralSystem.String - Reference Type Pin
zecodela12-Jan-05 22:38
zecodela12-Jan-05 22:38 
GeneralRe: System.String - Reference Type Pin
Sujith S12-Jan-05 23:03
Sujith S12-Jan-05 23:03 
GeneralRe: System.String - Reference Type Pin
Stefan Troschuetz12-Jan-05 23:06
Stefan Troschuetz12-Jan-05 23:06 
GeneralRe: System.String - Reference Type Pin
Adam Goossens13-Jan-05 0:32
Adam Goossens13-Jan-05 0:32 
From what I am aware of a System.String is a reference type, yes, but it's also immutable - i.e., the character sequence in it cannot be changed once it is created. Methods that appear to change a string only return a new string with the modifications made.

Note: everything below here is my interpretation of what is going on. Smile | :)

When you do something like this:

string a = "one";
string b;

b = a;


a and b will point to the same location for the string in memory - just like you expect with reference types. The catch is that because a string is immutable, as soon as you do this:

b = "two";


The CLR will now remove b's reference to a, create a new String object (with the character sequence "two") and assign that to b. a and b now point to two entirely different System.String's in memory. This process of sharing a reference until someone makes a change helps keep memory usage down whilst keeping a System.String inherantly immutable - two strings will point to each other until one of them changes. When one of them changes, two entirely seperate strings are created.

Even when you pass a string by reference (using ref) you still aren't actually modifying the original string. All you are doing is creating a new System.String in memory somewhere and changing the ref'd variable's reference to point to this new string. The old string still exists in memory somewhere, but providing certain conditions are met it's up for garbage collection now.

<br />
public void Foo()<br />
{<br />
    string one="two";<br />
    Bar(ref one); // pass in the refence to the variable "one". Does not pass the string.<br />
}<br />
public void Bar(ref string inStr)<br />
{<br />
    // create a new System.String in memory, set it to the character <br />
    // sequence "three", and change inStr's reference to point to this new string.<br />
    inStr = "three"; <br />
}<br />


This space for rent!
My Blog
GeneralC# with XML Pin
Newbie_Toy12-Jan-05 22:09
Newbie_Toy12-Jan-05 22:09 
GeneralRe: C# with XML Pin
Adam Goossens13-Jan-05 0:41
Adam Goossens13-Jan-05 0:41 
GeneralC# operator overloading Pin
ting66812-Jan-05 22:01
ting66812-Jan-05 22:01 
GeneralRe: C# operator overloading Pin
Stefan Troschuetz12-Jan-05 22:34
Stefan Troschuetz12-Jan-05 22:34 
GeneralRe: C# operator overloading Pin
J4amieC13-Jan-05 2:48
J4amieC13-Jan-05 2:48 
GeneralRe: C# operator overloading Pin
turbochimp13-Jan-05 3:06
turbochimp13-Jan-05 3:06 
Generalhelp on openwith dialog Pin
deepakskumar12-Jan-05 21:17
deepakskumar12-Jan-05 21:17 
GeneralRe: help on openwith dialog Pin
Adam Goossens13-Jan-05 0:44
Adam Goossens13-Jan-05 0:44 
GeneralBackColor for MainMenu Pin
hybrid7912-Jan-05 19:39
hybrid7912-Jan-05 19:39 
Generalproblem with fontdialog Pin
The Nemesis12-Jan-05 19:33
The Nemesis12-Jan-05 19:33 
GeneralSmartClient Pin
IamADotNetGuy12-Jan-05 18:44
IamADotNetGuy12-Jan-05 18:44 
GeneralRe: SmartClient Pin
Michael P Butler13-Jan-05 9:04
Michael P Butler13-Jan-05 9:04 
Generalbinary file reading and converting Pin
bwagz12-Jan-05 18:19
bwagz12-Jan-05 18:19 
Generalerror in this c# program Pin
dhol12-Jan-05 17:55
dhol12-Jan-05 17:55 
GeneralRe: error in this c# program Pin
leppie12-Jan-05 21:49
leppie12-Jan-05 21:49 
GeneralRe: error in this c# program Pin
dhol13-Jan-05 1:03
dhol13-Jan-05 1:03 
GeneralRe: error in this c# program Pin
leppie13-Jan-05 4:57
leppie13-Jan-05 4:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.