我正在Unity c#項目中工作,我得到一個錯誤,我不明白他爲什麼這麼說。爲什麼我的編譯器在將const default添加到構造函數參數後識別某些方法?
這些錯誤指向哪裏調用該方法SaveDictionary()和LoadDictionary()在下列文件中的地方:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BinaryDictionary<T>
{
public BinaryDictionary(string name, string path = FileReadWrite.binPath)
{
fileName = name;
this.path = path;
}
protected Dictionary<string, T> dict;
protected string fileName;
protected string path;
protected virtual void LoadDictionary()
{
if (dict == null)
{
try
{
dict = FileReadWrite.GetBinaryFile<Dictionary<string, T>>(fileName, path);
}
catch (System.IO.FileNotFoundException e)
{
Debug.LogWarning(fileName + " not found, making new Dictionary.");
dict = new Dictionary<string, T>();
}
}
}
public virtual void SaveDictionary()
{
if (dict != null)
{
FileReadWrite.SaveBinaryFile(fileName, path, dict);
}
}
public T GetElement(string key)
{
LoadDictionary();
T toReturn = dict.ContainsKey(key) ? dict[key] : default(T);
return toReturn;
}
public void AddElement(string key, T value)
{
LoadDictionary();
if (!dict.ContainsKey(key))
{
dict.Add(key, value);
}
else
{
dict[key] = value;
}
SaveDictionary();
}
public void RemoveElement(string key)
{
LoadDictionary();
if (dict.ContainsKey(key))
{
dict.Remove(key);
SaveDictionary();
}
}
public Dictionary<string, T>.KeyCollection GetKeys()
{
LoadDictionary();
return new Dictionary<string, T>(dict).Keys;
}
public Dictionary<string, T> GetDictionary()
{
LoadDictionary();
return new Dictionary<string, T>(dict);
}
}
起初這些錯誤沒有顯示。當我在構造函數中爲參數「path」添加默認值FileReadWrite.binPath時,它們出現了。
我宣佈FileReadWrite.binPath如下:
public const string binPath = "Other/Bin/";
當我再次刪除默認值(在構造函數),這些錯誤消失。
有人知道我做錯了什麼,或者他爲什麼說錯誤在這種情況下?
它編譯我,我不得不刪除FileReadWrite.GetBinaryFile和FileReadWrite.SaveBinaryFile調用,也許是錯誤的FileReadWrite。 – Atomosk
FileReadWrite是一個公共靜態類。 –