Resharper wanted me to change this code:
foreach (var item in cmbxColor1.Items)
{
cmbxColor2.Items.Add(item);
. . .
…because it said, “Possible ‘System.NullReferencesException'”
So it should be this:
foreach (var item in cmbxColor1.Items)
{
if (null != cmbxColor2.Items)
{
cmbxColor2.Items.Add(item);
. . .
?
I don’t get this – how could a combobox’s Items be null, unless null == empty? And if null == empty, then that’s exactly what they [s,w]ould be when this code is called.
I suppose Resharper is wrong here, as
Itemscollection of theComboBoxseems to be initialized by its constructor. That is:is guaranteed to be OK.
Also only
getaccessor is available for us here so that no one could replace this collection with another (ornull). Though I am no quite sure, if there were a possibility to influence this collection while deriving fromComboBox(I couldn’t find smth at once), I guess even then it still remains guaranteed not null.