不能含蓄转换类型'诠释'到'字符串' [英] Cannot implicity convert type 'int' to 'string'

查看:155
本文介绍了不能含蓄转换类型'诠释'到'字符串'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 随机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(); } the return val ^ dkey shows the error

Cannot implicitly convert type 'int' to 'string'

解决方案

Your GetScore method returns a string, but val ^ 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屋!

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