[英]Is it good to use a static method and static class to read and write to a json file?
我正在嘗試從 JSON 文件中讀寫。 使用 static 方法好嗎?如何提高這段代碼的性能
public static class Service
{
private static JsonContainer ReadAll()
{
using (StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(Constants.JsonFilePath)))
{
return JsonConvert.DeserializeObject<JsonContainer>(sr.ReadToEnd());
}
}
private static bool WriteAll(JsonContainer data)
{
// serialize JSON directly to a file
using (StreamWriter file = new StreamWriter(HttpContext.Current.Server.MapPath(Constants.JsonFilePath)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, data);
return true;
}
}
}
一些額外的點:
除了Math
之外,我真的想不出任何其他 class 有意義(我敢肯定有一些),那是因為這些概念是核心,不要改變,也沒有擴展。
任何其他重要的表示都不會從中受益。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.