0

With C#8 and null reference turn on, I now get this warning, item is highlighted with Converting null literal or possible null value to non nullable type. The Class rtGirdProp has no non nullable items. How do I fix this?

enter code here
                foreach (rtGridProp item in items)
                {
                    shares = shares + item.rtTotalShare;
                    totalPrice = totalPrice + (item.rtTotalShare * item.rtAverage);
                }
                if (shares == 0)
                    this.AveragePrice = 0;
                else
                    this.AveragePrice = totalPrice / shares;
Jason
  • 81
  • 6

1 Answers1

0

Only if you are sure that it is not potentially harmful (The non-nullable variable assigned from null literal or possible null value is guaranteed not to be used until checked/reassigned from another non-null value, or the possible null value is guaranteed to be non-null at that moment.),

you can mark it as a non-null value with the trailing !.

object a = null!;
Alsein
  • 4,268
  • 1
  • 15
  • 35