Null array in a dictionary
-
I need to instatiate a dictionary accepting null as value, but I have a syntax problem:
Dictionary d = new Dictionary()
d.Add("a", new double[2]{100,102});
d.Add("b", null);
d.Add("v", new double[2] { 99, 101 });;Is it possible to do it?
-
I need to instatiate a dictionary accepting null as value, but I have a syntax problem:
Dictionary d = new Dictionary()
d.Add("a", new double[2]{100,102});
d.Add("b", null);
d.Add("v", new double[2] { 99, 101 });;Is it possible to do it?
-
I need to instatiate a dictionary accepting null as value, but I have a syntax problem:
Dictionary d = new Dictionary()
d.Add("a", new double[2]{100,102});
d.Add("b", null);
d.Add("v", new double[2] { 99, 101 });;Is it possible to do it?
You don't need the '?' at all - you need that to specify a value type that can hold a null value. Reference types automatically can (as this is the default value for a reference type variable) All arrays are reference types, regardless of whether they are array of reference or value types. So what you need to say is:
Dictionary<string, double[]> d = new Dictionary<string, double[]>();
d.Add("a", new double[2] { 100, 102 });
d.Add("b", null);
d.Add("v", new double[2] { 99, 101 });You would only need the '?' if you wanted an array of doubles that could hold null values, and then the syntax would be:
double?[] da = new double?[10];
To indicate that the individual elements of the array could contain nulls.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
You don't need the '?' at all - you need that to specify a value type that can hold a null value. Reference types automatically can (as this is the default value for a reference type variable) All arrays are reference types, regardless of whether they are array of reference or value types. So what you need to say is:
Dictionary<string, double[]> d = new Dictionary<string, double[]>();
d.Add("a", new double[2] { 100, 102 });
d.Add("b", null);
d.Add("v", new double[2] { 99, 101 });You would only need the '?' if you wanted an array of doubles that could hold null values, and then the syntax would be:
double?[] da = new double?[10];
To indicate that the individual elements of the array could contain nulls.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
Thanks in this way I'm not able to find the Max value of first element
var ks = d.Max(v => v.Value[0]);
I didn't clarify before this port sorry
-
Thanks in this way I'm not able to find the Max value of first element
var ks = d.Max(v => v.Value[0]);
I didn't clarify before this port sorry
Then all you need to do is change the lambda expression to allow for the null value:
var ks = d.Max(v => v.Value == null ? 0.0 : v.Value\[0\]);
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Then all you need to do is change the lambda expression to allow for the null value:
var ks = d.Max(v => v.Value == null ? 0.0 : v.Value\[0\]);
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
thanks in this case I see you need a "hard coded" value 0.0. Is it possible to say "if null just skip it"?
-
thanks in this case I see you need a "hard coded" value 0.0. Is it possible to say "if null just skip it"?
No, there is no way to "miss out" a value in a IEnumerable iteration. You could use double.MinValue instead:
var ks = d.Max(v => v.Value == null ? double.MinValue : v.Value\[0\]);
or
var ks = d.Max(v => { if (v.Value == null) return null; return v.Value\[0\]; });
But then you need to check for null in the ks variable as a list with all nulls with return a null instead of a double value.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
I need to instatiate a dictionary accepting null as value, but I have a syntax problem:
Dictionary d = new Dictionary()
d.Add("a", new double[2]{100,102});
d.Add("b", null);
d.Add("v", new double[2] { 99, 101 });;Is it possible to do it?
use like this Dictionary[]> d = new Dictionary[]>(); d.Add("a", new Nullable[2] { 100, 102 }); d.Add("b", null); d.Add("v", new Nullable[2] { 99, 101 }); ; It would help you. :)
-
thanks in this case I see you need a "hard coded" value 0.0. Is it possible to say "if null just skip it"?
You can use the Where method[^] to skip the null items:
var ks = d.Where(v => v.Value != null).Max(v => v.Value[0]);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
No, there is no way to "miss out" a value in a IEnumerable iteration. You could use double.MinValue instead:
var ks = d.Max(v => v.Value == null ? double.MinValue : v.Value\[0\]);
or
var ks = d.Max(v => { if (v.Value == null) return null; return v.Value\[0\]; });
But then you need to check for null in the ks variable as a list with all nulls with return a null instead of a double value.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
-
Hadn't thought of Where - good point!
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)