简体   繁体   English

如何传递函数返回的“修饰符”字符串?

[英]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. 通过另一个String对象进行的分配是不可接受的。 So how will the copy is created. 那么如何创建副本。

You're trying to write C++ code in C#. 您正在尝试用C#编写C ++代码。 You'll need to approach this the C# way. 您将需要使用C#方式进行处理。 Add a property for test.s, instead of a method called "get()". 为test.s添加一个属性,而不是一个名为“ get()”的方法。 We'll call this new property "MyString" in the code below: 我们将在以下代码中将此新属性称为“ MyString”:

class test
{
    public String MyString
    {
        get
        {
            return s;
        }

        set
        {
            s = value;
        }
    }

    private String s = "World"; 
}

class modifier
{
    public static string modify(String v)
    {
        return v + "_test";
    }
}

test c = new test();
c.MyString = modifier.modify(c.MyString);

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! 如果您不喜欢Matthew Watson的答案并且想要坚持使用自己的方法,则可以使用StringBuilder,这是一个可以更改其值而无需创建新值的String!

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. 我不确定这是否足够清楚,但是如果您想使用Strings,则必须了解这一点。这正是s1无法更改其值的原因。

The workaround is with StringBuilder: 解决方法是使用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 在使用代码之前,请先了解String和StringBuilder的工作原理

You have to create a temporary variable: 您必须创建一个临时变量:

string tmp = c.get();
modifier.modify(ref tmp);

Because you are passing your parameter by reference. 因为您要通过引用传递参数。

The class test is designed such that its field s can't be reassigned (changed to point to a new object) from outside the class. test的设计使其不能从类外部重新分配其字段s (更改为指向新对象)。 That's because s is private , and the get() method only returns s , hence can't reassign it. 这是因为sprivate ,而get()方法仅返回 s ,因此无法重新分配它。

Either change the class test to allow the outside world to reassign s somehow, or use reflection to access a private field from the outside. 要么改变类的test ,让外界重新分配s不知何故,或使用反射来访问private从外场。

You can use a property with a get/set-eccessor and then pass to the modify() a test object: 您可以将属性与get / set-cesser一起使用 ,然后将test对象传递给Modify modify()

class test
{
    public String myString 
    { 
         get { return s; } 
         set { s = value; } 
    }

    private String s="World";
}

class modifier
{
    public static void modify(test myTest)
    {
        myTest.myString += "_test";
    } 
}

test c = new test();
modifier.modify(c);

Console.WriteLine(c.myString); //World_test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM