C# 调用需要对象数组的方法
我正在学习C#,并编写了一个控制台程序,将一系列高分保存到一个文件中。虽然这个程序可以运行,但我是如何让它运行的让我感到不安,感觉更像是一个黑客而不是一个解决方案,所以我一直在寻找如何编写它的指导 我目前在主要方法中所做的是:C# 调用需要对象数组的方法,c#,oop,C#,Oop,我正在学习C#,并编写了一个控制台程序,将一系列高分保存到一个文件中。虽然这个程序可以运行,但我是如何让它运行的让我感到不安,感觉更像是一个黑客而不是一个解决方案,所以我一直在寻找如何编写它的指导 我目前在主要方法中所做的是: 声明highscore对象的数组 初始化它们 为数组指定一些值 到目前为止,我对自己所做的一切感到高兴,以下两个步骤让我感到不安 然后我声明另一个HighScore对象 我使用这个对象将highscores数组传递给SaveHighScores方法 这是我的密码: usi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace HighScore
{
class HighScore
{
public string Name { get; set; }
public int Score { get; set; }
public void SaveHighScores(HighScore[] highScores)
{
string allHighScoresText = "";
foreach (HighScore score in highScores)
{
allHighScoresText += $"{score.Name},{score.Score}" + Environment.NewLine;
}
File.WriteAllText("C:/Temp/highscores.csv", allHighScoresText);
}
static void Main(string[] args)
{
HighScore[] highScore = new HighScore[2];
for (int i = 0; i < highScore.Length; i++)
{
highScore[i] = new HighScore();
}
highScore[0].Name = "A";
highScore[0].Score = 100;
highScore[1].Name = "B";
highScore[1].Score = 200;
// are the following two lines correct or am I missing something?
HighScore hs = new HighScore();
hs.SaveHighScores(highScore);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
命名空间高分
{
班级高分
{
公共字符串名称{get;set;}
公共整数分数{get;set;}
公开作废保存高分(高分[]高分)
{
字符串allHighScoresText=“”;
foreach(高分中的高分)
{
allHighScoresText+=$“{score.Name},{score.score}”+Environment.NewLine;
}
File.writealText(“C:/Temp/highscores.csv”,allHighScoresText);
}
静态void Main(字符串[]参数)
{
HighScore[]HighScore=新的HighScore[2];
for(int i=0;i
Makesavehighscore
static,您不需要使用HighScore
的实例来调用它。(您可以直接将其称为HighScore.SaveHighScores()
)我更喜欢将数据的表示形式与对该数据执行的操作分开。因此,我将使用两个类,一个用于数据,另一个用于保存/加载和其他业务逻辑
public class HighScore
{
public string Name { get; set; }
public int Score { get; set; }
}
// This class handles the core work to persist your data on the storage medium
// The class is static so you don't need to declare instances and use directly the methods available.
public static class Repo_HighScore
{
// For simplicity, no error Handling but, for a robust implementation,
// error handling is required
public static bool SaveHighScores(HighScore[] highScores)
{
StringBuilder allHighScoresText = new StringBuilder();
foreach (HighScore score in highScores)
allHighScoresText.AppendLine($"{score.Name},{score.Score}");
File.WriteAllText("C:/Temp/highscores.csv", allHighScoresText.ToString());
}
public static HighScore[] LoadHighScores()
{
List<HighScore> hs = new List<HighScore>();
foreach(string line in File.ReadLines("C:/Temp/highscores.csv"))
{
string[] parts = line.Split(',');
HighScore temp = new HighScore()
{ Name = parts[0], Score = Convert.ToInt32(parts[1])};
hs.Add(temp);
}
return hs.ToArray();
}
}
公共课高分
{
公共字符串名称{get;set;}
公共整数分数{get;set;}
}
//此类处理将数据持久保存在存储介质上的核心工作
//该类是静态的,因此不需要声明实例并直接使用可用的方法。
公共静态类回购高分
{
//为了简单起见,没有错误处理,但是为了实现健壮性,
//需要进行错误处理
公共静态bool SaveHighScores(highScores[]highScores)
{
StringBuilder allHighScoresText=新StringBuilder();
foreach(高分中的高分)
allHighScoresText.AppendLine($“{score.Name},{score.score}”);
writealText(“C:/Temp/highscores.csv”,allHighScoresText.ToString());
}
公共静态高分[]加载高分()
{
List hs=新列表();
foreach(File.ReadLines(“C:/Temp/highscores.csv”)中的字符串行)
{
string[]parts=line.Split(',');
HighScore temp=新的HighScore()
{Name=parts[0],Score=Convert.ToInt32(parts[1]);
hs.添加(临时);
}
返回hs.ToArray();
}
}
是否有理由将SaveHighScore作为HighScore的一部分。这可能只是一个静态方法。你的问题似乎更适合CodeReview而不是这个site@Steve这是一个正确的观点。我不确定这类问题应该存在于何处。好吧,您的实现近乎于一个bug。它的工作原理是“错误的”,我也是。我只是模糊地知道在那个网站上发布的规则。非常感谢,非常感谢。我需要把它连同评论一起拿走,这样我才能理解它们。加上LoadHighScores方法,你知道我在看《C#Player指南》吗?什么?不,我甚至不知道它的存在。你是说有一种叫做LoadHighScores的方法吗?我是否侵犯了某些版权?:-)我觉得这是鼓励不良做法,下面的答案更好。