如何将var转换为字符串c# [英] How to convert a var to a string c#

查看:228
本文介绍了如何将var转换为字符串c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在使用buddy.com为我的windows商店应用创建一个排行榜

i面临在排行榜中获得前十名高分的问题

< br $>

hello , i am creating a leaderboard for my windows store app using buddy.com
i am facing a problem in getting the top ten high scores in the leaderboard

var highestScore = highScores.First().Score;



i使用此代码获得第一个得分,这个有效但当我试图这样做时。


i used this code to get the first score and this worked but when i tried to do this .

var top10 = highScores.Take(10);
           foreach (var myscore in top10) {

               string myresult = myscore.Score.ToString();
               secondscore.Text = myresult;

           }





i只获得5但我应该得到一个包含前10个分数的字符串,我的代码有什么问题以及如何修复它,谢谢



i get 5 only but i should get a string contain the top 10 scores , what is wrong in my code and how to fix it , thank you

推荐答案

嗯......这有点明显。

什么这段代码是这样做的:

Well...it's a little obvious.
What does this code do:
int value = 0;
for (int i = 1; i < 10; i++)
   {
   value = i;
   }
Console.WriteLine(index);

答案:打印9。

为什么?因为整数只能包含一个值,所以当你说:

Answer: it prints "9".
Why? Because an integer can contain only one value, so when you say:

value = i;

你丢弃以前的版本。

现在,你的代码也是这样:

you discard the previous version.
Now, your code does the same:

foreach (var myscore in top10) {
    string myresult = myscore.Score.ToString();
    secondscore.Text = myresult;
}

MyResult和secondscore.Text都是字符串,所以当你为它们分配一个新值时,你会抛弃前一个值。



你想要做什么,我不确定,只需将其更改为:

Both MyResult and secondscore.Text are strings, so when you assign them a new value, you discard every previous one.

Exactly what you want to do, I'm not sure, but just changing it to:

foreach (var myscore in top10) {
    string myresult = myscore.Score.ToString();
    secondscore.Text += myresult;
}

会告诉你我的意思。


这篇关于如何将var转换为字符串c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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