C# 无法将类型VOID隐式转换为string

来源:百度知道 编辑:UC知道 时间:2024/06/22 23:36:33
static void Main(string[] args)
{
string name;
string camelName;

Console.WriteLine("请输入一个字符串,各单词以空格分隔:");
name = Console.ReadLine();

camelName = ConvertToPascal(ref name);
Console.WriteLine("按Pascal规则转换后的名称是:{0}",camelName);

Console.ReadLine();
}

private static void ConvertToPascal(ref string name)
{
string camelName;
string[] words;
string tempWord;
string answer;

words = name.Split(' ');

camelName = words[0].ToUpper();

do{
for (int i = 1; i < words.Length; i++)
{
tempWord = words[i].Substring(0, 1).ToUpper();
camelName = camelName + tempWord

因为private static void ConvertToPascal(ref string name)这个方法你的返回值类型是Void 类型,如果你要使用这个方法返回一个字符串的话,那么需要修改返回值类型为string,也就是:
private static string ConvertToPascal(ref string name)

在方法的最后用关键字 return 返回你需要得到的字符串就可以。

路过~希望可以帮助你~

直接写成ConvertToPascal(ref camelName);
不用写 camelName =......