不能含蓄转换类型'诠释'到'字符串' [英] Cannot implicity convert type 'int' to 'string'
问题描述
随机randgen =新的随机();
INT DKEY;
公共对象SetScore(INT VAL)
{
DKEY = randgen.Next(int.MaxValue / 2);
返回VAL ^ DKEY; // ^意味着XOR。
}
公共字符串GetScore(INT VAL)
{
返回VAL ^ DKEY;
GC.Collect的();
}
从公共字符串GetScore(INT VAL)
的
{
返回VAL ^ DKEY;
GC.Collect的();
} 返回VAL ^ DKEY
显示错误
无法隐式转换类型'诠释'到'字符串'
块引用>
解决方案您
GetScore
方法返回一个字符串,但VAL ^ DKEY
是一个整数。无论是结果转换为字符串:收益率(VAL ^ DKEY)的ToString();
或返回一个int:
公众诠释GetScore(INT VAL)...
哦,请删除调用
GC.Collect的()
。这是几乎从来没有一个好主意,你自己调用它。至少它的精心放置在一个地方不可达...Random randgen = new Random(); int dkey; public object SetScore(int val) { dkey=randgen.Next(int.MaxValue/2); return val ^ dkey; //^ means XOR. } public string GetScore(int val) { return val ^ dkey; GC.Collect(); }
From
public string GetScore(int val) { return val ^ dkey; GC.Collect(); }
thereturn val ^ dkey
shows the errorCannot implicitly convert type 'int' to 'string'
解决方案Your
GetScore
method returns a string, butval ^ dkey
is an integer. Either convert the result to string with:return (val ^ dkey).ToString();
or return an int:
public int GetScore(int val) ...
Oh, and please remove the call to
GC.Collect()
. It's almost never a good idea to call it yourself. At least it's carefully placed in an unreachable place...这篇关于不能含蓄转换类型'诠释'到'字符串'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!