C# 为什么';在将const default添加到构造函数参数后,我的编译器是否识别某些方法?
我在Unity c#做一个项目,我犯了一个错误,我不明白他为什么这么说 这些错误指向以下文件中调用方法SaveDictionary()和LoadDictionary()的位置:C# 为什么';在将const default添加到构造函数参数后,我的编译器是否识别某些方法?,c#,unity3d,compiler-errors,C#,Unity3d,Compiler Errors,我在Unity c#做一个项目,我犯了一个错误,我不明白他为什么这么说 这些错误指向以下文件中调用方法SaveDictionary()和LoadDictionary()的位置: using UnityEngine; using System.Collections; using System.Collections.Generic; public class BinaryDictionary<T> { public BinaryDictionary(string name,
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);
}
}
当我再次删除默认值(在构造函数中)时,这些错误就会消失
有人知道我做错了什么,或者他为什么在这种情况下说这个错误吗?您可以尝试不使用
FileReadWrite.binPath
作为构造函数的默认参数。请尝试以下方法:
public BinaryDictionary(string name, string path = null)
{
fileName = name;
if(path == null)
{
this.path = FileReadWrite.binPath;
}
}
我不知道确切的问题,但我猜您的虚拟方法正在导致与构造函数的冲突。(不确定,只是猜测)您可以尝试不使用
FileReadWrite.binPath
作为构造函数的默认参数。请尝试以下方法:
public BinaryDictionary(string name, string path = null)
{
fileName = name;
if(path == null)
{
this.path = FileReadWrite.binPath;
}
}
我不知道确切的问题,但我猜您的虚拟方法正在导致与构造函数的冲突。(不确定,只是猜测)它为我编译,而我必须删除
FileReadWrite.GetBinaryFile
和FileReadWrite.SaveBinaryFile
调用,可能是FileReadWrite
有问题。FileReadWrite是一个公共静态类。它为我编译,虽然我不得不删除FileReadWrite.GetBinaryFile
和FileReadWrite.SaveBinaryFile
调用,但可能是FileReadWrite
有问题。FileReadWrite是一个公共静态类。重新启动Unity编辑器并进行Mono。重新启动,没有任何更改(我使用的是Visual Studio,不是MonoDevelop:))重新启动Unity编辑器和Mono。重新启动,没有更改(我使用的是Visual Studio,不是MonoDevelop:)