C# 返回空数组而不是null
我创建了C# 返回空数组而不是null,c#,C#,我创建了Settings类,用于通过.ini文件编辑我的应用程序。我的Settings.ini文件如下所示: [ACCOUNT] login=xyz password=xyz locations=1,2,5,8 class Settings { public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini"); public static string L
Settings
类,用于通过.ini
文件编辑我的应用程序。我的Settings.ini
文件如下所示:
[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8
class Settings {
public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini");
public static string Login { get { return Config.Read("login", "ACCOUNT"); } set { Config.Write("login", "ACCOUNT"); } }
public static string Password { get { return Config.Read("password", "ACCOUNT"); } set { Config.Write("password", "ACCOUNT"); } }
public static int[] Locations { get { return Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s)); } set { Config.Write("locations", "ACCOUNT"); } }
}
public static int[] Locations
{
get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; }
set { Config.Write("locations", "ACCOUNT"); }
}
public static int[] Locations
{
get
{
int[] values = Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','),
s => int.Parse(s)) ?? new int[] { };
return values;
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
public static int[] Locations
{
get
{
string locations = Config.Read("locations", "ACCOUNT");
if (locations == null)
{
return new int[0];
}
return locations
.Split(',') // split the locations separated by a comma
.Select(int.Parse) // transform each string into the corresponding integer
.ToArray(); // put the resulting sequence (IEnumerable<int>) into an array of integers
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
现在我得到了如下这些值:
[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8
class Settings {
public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini");
public static string Login { get { return Config.Read("login", "ACCOUNT"); } set { Config.Write("login", "ACCOUNT"); } }
public static string Password { get { return Config.Read("password", "ACCOUNT"); } set { Config.Write("password", "ACCOUNT"); } }
public static int[] Locations { get { return Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s)); } set { Config.Write("locations", "ACCOUNT"); } }
}
public static int[] Locations
{
get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; }
set { Config.Write("locations", "ACCOUNT"); }
}
public static int[] Locations
{
get
{
int[] values = Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','),
s => int.Parse(s)) ?? new int[] { };
return values;
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
public static int[] Locations
{
get
{
string locations = Config.Read("locations", "ACCOUNT");
if (locations == null)
{
return new int[0];
}
return locations
.Split(',') // split the locations separated by a comma
.Select(int.Parse) // transform each string into the corresponding integer
.ToArray(); // put the resulting sequence (IEnumerable<int>) into an array of integers
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
问题是,当我的Settings.ini
文件的位置为空时:
locations=
我的变量Settings.Locations
返回null
而不是空数组。我试过这样做:
[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8
class Settings {
public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini");
public static string Login { get { return Config.Read("login", "ACCOUNT"); } set { Config.Write("login", "ACCOUNT"); } }
public static string Password { get { return Config.Read("password", "ACCOUNT"); } set { Config.Write("password", "ACCOUNT"); } }
public static int[] Locations { get { return Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s)); } set { Config.Write("locations", "ACCOUNT"); } }
}
public static int[] Locations
{
get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; }
set { Config.Write("locations", "ACCOUNT"); }
}
public static int[] Locations
{
get
{
int[] values = Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','),
s => int.Parse(s)) ?? new int[] { };
return values;
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
public static int[] Locations
{
get
{
string locations = Config.Read("locations", "ACCOUNT");
if (locations == null)
{
return new int[0];
}
return locations
.Split(',') // split the locations separated by a comma
.Select(int.Parse) // transform each string into the corresponding integer
.ToArray(); // put the resulting sequence (IEnumerable<int>) into an array of integers
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
但这是行不通的。我无法将int[]转换为int。您知道如何返回空数组吗?首先,您在一行中插入的内容太多,因此很难读取,更不用说故障排除了。你需要的是这样的东西:
[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8
class Settings {
public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini");
public static string Login { get { return Config.Read("login", "ACCOUNT"); } set { Config.Write("login", "ACCOUNT"); } }
public static string Password { get { return Config.Read("password", "ACCOUNT"); } set { Config.Write("password", "ACCOUNT"); } }
public static int[] Locations { get { return Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s)); } set { Config.Write("locations", "ACCOUNT"); } }
}
public static int[] Locations
{
get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; }
set { Config.Write("locations", "ACCOUNT"); }
}
public static int[] Locations
{
get
{
int[] values = Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','),
s => int.Parse(s)) ?? new int[] { };
return values;
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
public static int[] Locations
{
get
{
string locations = Config.Read("locations", "ACCOUNT");
if (locations == null)
{
return new int[0];
}
return locations
.Split(',') // split the locations separated by a comma
.Select(int.Parse) // transform each string into the corresponding integer
.ToArray(); // put the resulting sequence (IEnumerable<int>) into an array of integers
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
注意,我添加了??在调用的第一条语句末尾添加新的int[]{}
。如果另一个数组为null,它将返回一个空数组
这是一个偏好的问题,但我将getter分成两行的原因是,我可以在它返回之前进行调试和中断,以观察返回值。您也可以在最后一个括号中打断,并在“局部变量”窗口中观察返回值。您可以这样显式执行:
[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8
class Settings {
public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini");
public static string Login { get { return Config.Read("login", "ACCOUNT"); } set { Config.Write("login", "ACCOUNT"); } }
public static string Password { get { return Config.Read("password", "ACCOUNT"); } set { Config.Write("password", "ACCOUNT"); } }
public static int[] Locations { get { return Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s)); } set { Config.Write("locations", "ACCOUNT"); } }
}
public static int[] Locations
{
get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; }
set { Config.Write("locations", "ACCOUNT"); }
}
public static int[] Locations
{
get
{
int[] values = Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','),
s => int.Parse(s)) ?? new int[] { };
return values;
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
public static int[] Locations
{
get
{
string locations = Config.Read("locations", "ACCOUNT");
if (locations == null)
{
return new int[0];
}
return locations
.Split(',') // split the locations separated by a comma
.Select(int.Parse) // transform each string into the corresponding integer
.ToArray(); // put the resulting sequence (IEnumerable<int>) into an array of integers
}
set
{
Config.Write("locations", "ACCOUNT");
}
}
公共静态int[]位置
{
得到
{
字符串位置=Config.Read(“位置”、“帐户”);
如果(位置==null)
{
返回新整数[0];
}
返回位置
.Split(“,”)//分割用逗号分隔的位置
.Select(int.Parse)//将每个字符串转换为相应的整数
.ToArray();//将结果序列(IEnumerable)放入整数数组中
}
设置
{
配置写入(“位置”、“账户”);
}
}
为什么要声明值而不使用它?除了缺少的代码>我收到了格式异常
,但我没有注意到。您的代码也不能正常工作。我不明白,Config.Read(“位置”,“帐户”)
如果没有位置,则返回null?因为Locations
属性从不返回null,因为Array.ConvertAll
从不返回null。但是如果Config.Read
返回nullstring,Split
将抛出异常。你是对的。现在我注意到我得到的是格式异常
而不是数组。ConvertAll
您应该使用循环和int.TryParse
来尝试解析每个标记。然后,您可以处理无效的输入。我必须更改为:if(string.IsNullOrEmpty(works)){
并且它工作得很好。谢谢:)VS现在对此的抱怨。它建议Array.Empty();
。