I need to bind the results to a control that only excepts int[]
If the array in not empty how do I Cast the int?[] array to int[]
Your best bet is to use the ?? operator, probably, and foreach on the
int?[] to convert it to an int[]. A quick example:
===== BEGIN C# CODE =====
using System;
public class EntryPoint {
public static void Main() {
int?[] nullable = {0, null, 24, 42, 64, null, 7};
int[] nonnullable = new int[nullable.Length];
int counter = 0;
// This is the important part, and we see below how this works
foreach(int? val in nullable) {
nonnullable[counter++] = val ?? 0;
}
Console.Write("nullable: ");
foreach(int? i in nullable) {
if(i == null) {
Console.Write("(null), ");
} else {
Console.Write("{0}, ", i);
}
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();
Console.Write("nonnullable: ");
foreach(int i in nonnullable) {
Console.Write("{0}, ", i);
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();
}
}
===== END C# CODE =====
You can compile this and you'll get a list of numbers, one with (null)
values in them and the other with 0s instead of (null) values.
You can certainly write a simple method whose sole purpose in life is
to convert int?[] to int[]. The following I've tested as shown, and it
works. It ought to work with any other such type:
===== BEGIN C# CODE =====
using System;
public class EntryPoint {
public static void Main() {
int?[] nullable = {0, null, 24, 42, 64, null, 7};
int[] nonnullable = NullableToRegular<int>(nullable);
int counter = 0;
foreach(int? val in nullable) {
nonnullable[counter++] = val ?? 0;
}
Console.Write("nullable: ");
foreach(int? i in nullable) {
if(i == null) {
Console.Write("(null), ");
} else {
Console.Write("{0}, ", i);
}
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();
Console.Write("nonnullable: ");
foreach(int i in nonnullable) {
Console.Write("{0}, ", i);
}
Console.CursorLeft -= 2;
Console.Write(" ");
Console.WriteLine();
}
public static G[] NullableToRegular<G>(Nullable<G>[] items) where G :
struct {
G[] retval = new G[items.Length];
int counter = 0;
foreach(Nullable<G> item in items) {
retval[counter++] = item ?? default(G);
}
return(retval);
}
}
===== END C# CODE =====
There may be a .NET built-in method somewhere that does something like
this, I am not certain. But if not, this one should be pretty close to
what you're looking for. It only took me a few minutes to come up with
once I learned how to write a generic method. Incidentally, I used
"CLR via C#", 2nd ed., Microsoft Press to come up with the information
to write that generic method... good book, great reference.
HTH,
Mike