如何创建一个等于方法参数变量但我可以编辑它的变量? [英] How do I make a variable that is equal to a method parameter variable but I can edit it?

查看:74
本文介绍了如何创建一个等于方法参数变量但我可以编辑它的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的方法是正确的

So i have this method right

public static void poop(String hi){
 String hiBoss = hi;
 hiBoss.TrimStart();
}





我想创建一个等于参数的字符串,但我可以编辑它吗?

我怎么能这样做,它给我一个错误,只读它



and I want to make a string that is equal to the parameter but I can edit it?
how can i do that, it gives me an error that it is read only

推荐答案

有几种方法你可能想看看。

第一种方法是返回新值作为方法的结果:

There are a couple of ways you might want to look at.
The first is to return the new value as the result of the method:
public static string poop(string hi)
   {
   return hi.TrimStart();
   }

然后您可以这样使用它:

You would then use it like this:

string trimmed = poop("   Hello World!   ");
Console.WriteLine(trimmed);



第二种是传递对参数的引用:


The second is to pass a reference to the parameter:

public static void poop(ref string hi)
   {
   hi = hi.TrimStart();
   }

然后你就这样使用它:

And you then use it like this:

string untrimmed = "   Hello World!   ";
poop(ref untrimmed);
Console.WriteLine(untrimmed);

在这两种情况下,它都会在控制台上打印Hello World!。

In both cases, it will print "Hello World! " on the console.


这篇关于如何创建一个等于方法参数变量但我可以编辑它的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆