Search code examples
c#stringref

How to pass in the "modifier" string returned by the function?


class test
{
  public String get() {return s;}
  private String s="World";
}

class modifier
{
   public static void modify(ref String v)
   {
     v+="_test";
   }
}

String s1="Earth";


modifier.modify(ref s1); <-------- OK

test c=new test();
modifier.modify(ref c.get()); <------- Error

How to pass in the "modifier" string returned by the function? Assignment through another String object is unacceptable. So how will the copy is created.


Solution

  • If you dont like Matthew Watson's answer and you want to stay to your approach, the way to go is by using StringBuilder.That is a String that can change its value without creating a new one!

    What i mean:
    Normal String's value cant be changed, what is happening is that a new String is being created....and the pointer(that points to the String you want to change its value) points from now on to this new String. All other pointers....that "were pointing" to the old String .....are still pointing...to the old String!(the String's value didnt change!)
    I am not sure if thats clear enough but you have to understand this if you want to play with Strings.This is exactly why s1 cant change its value.

    The workaround is with StringBuilder:

        class test
    {
        public StringBuilder get() { return  s; }
        private StringBuilder s = new StringBuilder("World");
    }
    
    class modifier
    {
        public static void modify(StringBuilder v)
        {
            v.Append("_test");
        }
    }
    

    And some test code : (of course all this comes with processing cost...but i dont think that will be an issue for now)

            StringBuilder s1 = new StringBuilder("Earth");
    
            System.Diagnostics.Debug.WriteLine("earth is {0}", s1);
            modifier.modify(s1); //<-------- OK
            System.Diagnostics.Debug.WriteLine("earth is {0}",s1);
    
            test c=new test();
            StringBuilder aa=c.get();
            System.Diagnostics.Debug.WriteLine("earth is {0}", aa);
            modifier.modify(aa); //<------- Error(not anymore)
            System.Diagnostics.Debug.WriteLine("earth is {0}", c.get());
    

    Before you use the code try to understand how String and StringBuilder works