无法将类型字符串隐式转换为 System.Func<string,int,string,string>
Cannot implicity convert type string to System.Func<string,int,string,string>
当我使用方法替换字符串中的一个值时,这是有效的,但当我设置一个函数来执行此操作时,它却不起作用。方法和下面的func几乎一模一样
Func 给出错误
Cannot implicity string
to System.Func<string,int,string,string>
.
我知道。我知道。如果可行,请使用该方法并忘记 Func。只是想知道为什么 Func 不起作用。我花了一些时间尝试不同类型的组合但没有成功。我是一个新手,只是为了好玩而学习 C#(?)。
static Func<string,int,string,string> ReplaceNumber(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
string temp = sb.ToString();
return temp; // why doesn't "return sb.ToString()" work
}
static string ReplaceNumber(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
string temp = sb.ToString();
return temp; // why doesn't "return sb.ToString()" work
}
像这样更改 Func
:
static Func<string, int, string, string> ReplaceNumber = delegate(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
return sb.ToString();
};
并这样称呼他们:
string output = ReplaceNumber("Sample", 1, "sample op3"); // op will be "Ssample op3mple"
注意:return sb.ToString();
将在以下条件下工作,即 location
具有整数值,该整数值是字符串中的有效位置。
您的静态方法也会为您完成同样的任务:
static string ReplaceNumber(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
return sb.ToString();
}
当我使用方法替换字符串中的一个值时,这是有效的,但当我设置一个函数来执行此操作时,它却不起作用。方法和下面的func几乎一模一样
Func 给出错误
Cannot implicity
string
toSystem.Func<string,int,string,string>
.
我知道。我知道。如果可行,请使用该方法并忘记 Func。只是想知道为什么 Func 不起作用。我花了一些时间尝试不同类型的组合但没有成功。我是一个新手,只是为了好玩而学习 C#(?)。
static Func<string,int,string,string> ReplaceNumber(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
string temp = sb.ToString();
return temp; // why doesn't "return sb.ToString()" work
}
static string ReplaceNumber(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
string temp = sb.ToString();
return temp; // why doesn't "return sb.ToString()" work
}
像这样更改 Func
:
static Func<string, int, string, string> ReplaceNumber = delegate(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
return sb.ToString();
};
并这样称呼他们:
string output = ReplaceNumber("Sample", 1, "sample op3"); // op will be "Ssample op3mple"
注意:return sb.ToString();
将在以下条件下工作,即 location
具有整数值,该整数值是字符串中的有效位置。
您的静态方法也会为您完成同样的任务:
static string ReplaceNumber(string p, int location, string newValue)
{
StringBuilder sb = new StringBuilder();
sb.Append(p);
sb.Remove(location, 1);
sb.Insert(location, newValue);
return sb.ToString();
}