将变量返回到 C# 中的接收对象时出现问题
Problem returning a variable into a receiving object in C#
我在将变量共享给不同的对象时遇到了一点问题,我希望了解我做错了什么并找到解决问题的方法,我对使用该对象进行编程有点陌生面向编程语言 (C#),我不知道在细节方面我做错了什么。
我想做的基本上是创建一个方法,该方法接收用户输入的一些单词作为字符串,它从字符串中删除多余的 spaces 并创建一个字符串数组,其中包含每个字符串中由 space.
分隔的单词
这里的问题是“return 个单词;”在“RemoveSpace()”方法的末尾根本没有 returning 值到“Test()”。
(我确保使用调试器检查它是否确实在工作,但 RemoveSpace 函数肯定在工作,只是在方法内,并且 returned 值似乎被 Test() 方法忽略。)
(此外,要使用正则表达式,请使用“System.Text.RegularExpressions;”库)
我该怎么办?我已经绞尽脑汁思考了一段时间,但我没有主意,
我非常感谢您的帮助
public static class Reverso
{
static void Main(string[] args)
{
//User inputs the words as a string
string words = Console.ReadLine();
//Activates the Test object
Test(words);
}
public static void Test(string words)
{
//Activates the RemoveSpace object.
//It should receive the returned
//value here, but not working
RemoveSpace(words);
//Takes words into a string array
//seperated by spaces
string[] parts = words.Split(' ');
//Shows the result
Console.WriteLine(words);
}
public static string RemoveSpace(string words)
{
//Using regex in order to remove more
//than 1 space between words and characters
Regex regex = new Regex(@"[ ]{2,}", RegexOptions.None);
words = regex.Replace(words, @" ");
//Should return the value of the word
//to the Test object, *not working*
return words;
}
}
如果要在RemoveSpace
里面重新赋值给words
,需要通过引用传递变量:
public static string RemoveSpace(ref string words) // Use ref to pass by reference
然后在测试中,你可以这样调用方法:
RemoveSpace(ref words);
不通过引用传递,RemoveSpace
的words
参数是调用方法中words
变量的单独变量,因此重新分配只影响[=14的范围=].
通常的方法是在 Test
:
中重新分配从 RemoveSpace
返回的字符串
public static void Test(string words)
{
words = RemoveSpace(words);
我在将变量共享给不同的对象时遇到了一点问题,我希望了解我做错了什么并找到解决问题的方法,我对使用该对象进行编程有点陌生面向编程语言 (C#),我不知道在细节方面我做错了什么。
我想做的基本上是创建一个方法,该方法接收用户输入的一些单词作为字符串,它从字符串中删除多余的 spaces 并创建一个字符串数组,其中包含每个字符串中由 space.
分隔的单词这里的问题是“return 个单词;”在“RemoveSpace()”方法的末尾根本没有 returning 值到“Test()”。 (我确保使用调试器检查它是否确实在工作,但 RemoveSpace 函数肯定在工作,只是在方法内,并且 returned 值似乎被 Test() 方法忽略。) (此外,要使用正则表达式,请使用“System.Text.RegularExpressions;”库)
我该怎么办?我已经绞尽脑汁思考了一段时间,但我没有主意, 我非常感谢您的帮助
public static class Reverso
{
static void Main(string[] args)
{
//User inputs the words as a string
string words = Console.ReadLine();
//Activates the Test object
Test(words);
}
public static void Test(string words)
{
//Activates the RemoveSpace object.
//It should receive the returned
//value here, but not working
RemoveSpace(words);
//Takes words into a string array
//seperated by spaces
string[] parts = words.Split(' ');
//Shows the result
Console.WriteLine(words);
}
public static string RemoveSpace(string words)
{
//Using regex in order to remove more
//than 1 space between words and characters
Regex regex = new Regex(@"[ ]{2,}", RegexOptions.None);
words = regex.Replace(words, @" ");
//Should return the value of the word
//to the Test object, *not working*
return words;
}
}
如果要在RemoveSpace
里面重新赋值给words
,需要通过引用传递变量:
public static string RemoveSpace(ref string words) // Use ref to pass by reference
然后在测试中,你可以这样调用方法:
RemoveSpace(ref words);
不通过引用传递,RemoveSpace
的words
参数是调用方法中words
变量的单独变量,因此重新分配只影响[=14的范围=].
通常的方法是在 Test
:
RemoveSpace
返回的字符串
public static void Test(string words)
{
words = RemoveSpace(words);