Use of string.copy() ?

I know what string.copy() does. It creates a new copy of a string not a reference when you assign one string to another.... and I can see potential uses when you do string manipulation function... but I have a co-worker that uses string.copy() on all functions and properties that pass string value:

example:
public string DBSUser
{
get
{
return string.Copy(mDBUser);
}
set
{
mDBUser = String.Copy(value);
}
}

Can someone point me to some reading about string.copy() and potential uses



Answer this question

Use of string.copy() ?

  • sarika

    I'm a bit confused. If you run the below code, object, x, of type DoStuff has a member variable that is usable after x's destructor

    was called. This makes me believe the x is not dead. Is it If it is not dead, then if we change the property, Object, from ArrayList

    to another reference type, let's say string, then x was also still be alive. Wouldn't it Please, explain. When an object dies, does not all the member objects' memory release also

    ***************************code begin*******************************************

    public partial class Form1 : Form

    {

    private void test()

    {

    DoStuff x = new DoStuff();

    ArrayList al = x.Object;

    x = null;

    GC.Collect();

    al.Add("steve");

    Trace.WriteLine("al.Add(\"steve\")");

    }//private void test()

    private class DoStuff

    {

    private ArrayList mAL = new ArrayList();

    public DoStuff()

    {

    Trace.WriteLine("DoStuff");

    }//public DoStuff()

    ~DoStuff()

    {

    Trace.WriteLine("~DoStuff");

    }//~DoStuff()

    public ArrayList Object

    {

    get { return mAL; }

    }//public ArrayList Object

    }//private class DoStuff

    }//public partial class Form1 : Form

    ***************************code end *******************************************



  • Randal Greene

    Your question is off topic for the thread. Please post a new question in the C# forum and someone will answer it.

    Michael Taylor - 1/25/07
    http://p3net.mvps.org


  • LeeroyB

    There is no real benefit in using this method. In fact using it defeats the purpose of string anyway. Normally when you create a string if a string already exists with the same value then you'll get back a reference to the original string. This means that you have multiple references to the same string. Normally not a good thing but since strings are immutable whenever you change the value of the string you'll get a new copy while everyone else points to the original version. This saves space in memory. For example if you allocate a string containing all the letters of the alphabet and then assigned it to an array of 1000 elements then you'd have one string of size 26 and 1001 references to that string.

    However when you use Copy you actually create a brand new in-memory copy of the string. Therefore if you used Copy in the example above you'd have 1001 copies of the same string. Furthermore whenever the string value changed you'd get a new copy (because strings are immutable). C++ programmers (is your colleague one ) have this mindset and therefore are likely to want to copy the string value but .NET will handle all the details behind the scenes for you. Therefore you should never call Copy as you are defeating the purpose of .NET's string handling code and wasting space. I could only find 3 references to this method in all the framework code.

    Now that begs the question of why the method exists then. The only real use for this method is when doing interop work. If, for whatever reason, you must pass a pointer to a string to unmanaged code but you don't want other references to the same string to be modified you would have to copy the string first. Now if you use the default marshaler and specify the string as a ref string then the marshal will handle the details for you. So, in a nutshell, the method exists probably for internal use and very rarely in .NET code. It would probably be a good code review check to make sure that this method is never called in any code that you write. If it is then there is probably a problem somewhere (even with interop code).

    Michael Taylor - 1/24/07
    http://p3net.mvps.org


  • Use of string.copy() ?