I created Settings
class which I use to edit my application by .ini
file. My Settings.ini
file looks like this:
[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8
Now I am getting theese values like this:
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"); } }
}
The problem is, when my Settings.ini
file has empty locations:
locations=
My variable Settings.Locations
return null
instead of empty array. I've tried doing something like this:
public static int[] Locations
{
get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; }
set { Config.Write("locations", "ACCOUNT"); }
}
But that is just not working. I cant convert int[] to int. Do you have any ideas how can I return empty array?
Return Empty Array - With new int[0] or new Emp[0] When you want to return an array with a size of 0 then you just need to return new int[0]. Here, new int[0] returns an integer type array with zero values and size is 0.
It is best practice to return empty values rather than null ones. Especially when you return a collection, enumerable, or an object, you should avoid returning null. Returning null is okay if your code handles the returning null value. But developers may forget to write a null check.
null array—-when the size of array is not declared than the array is known as null array. EMPTY ARRAY——-if an array having the size but not values than it's known as empty array.
Just make: this. warenkorb = new PizzaV0[10]; This will initialize an empty array with 10 nulls.
Return an empty array or collection instead of a null value for methods that return an array or collection Some APIs intentionally return a null reference to indicate that instances are unavailable. This practice can lead to denial-of-service vulnerabilities when the client code fails to explicitly handle the null return value case.
We sometimes have to return an empty array for a few reasons, like when the array is coming from an API, and it returns null; in this case, we might want to return an array without any element, instead of null. Every array has a fixed size that we can specify when we create the array.
For methods that return a set of values using an array or collection, returning an empty array or collection is an excellent alternative to returning a null value, as most callers are better equipped to handle an empty set than a null value.
Prefer empty collection to null is a basic java principle that stems from Effective java Item 43: return empty arrays or collections, not nulls. There are a few reasons to support it:
You can do it explicitly like this:
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");
}
}
First of all, you're jamming too much into one line so it makes it really hard to read let alone troubleshoot. What you need is something like this:
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");
}
}
Notice I have added ?? new int[] { }
to the end of the first statement, which is called the null coalescing operator. It will return an empty array if the other array is null.
It's a matter of preference, but the reason I separated the getter into two lines is so I can debug and break before it returns to observe the return value. You could also break on the last bracket instead and observe the return value in the Locals window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With