Why does casting a null to a primitive(ie: int) in .net 2.0 throw a null ref exception and not a invalid cast exception?

前端 未结 4 890
生来不讨喜
生来不讨喜 2021-01-20 16:57

I was going through some code and came across a scenario where my combobox has not been initialized yet. This is in .NET 2.0 and in the following code, this.cbRegion.Select

4条回答
  •  再見小時候
    2021-01-20 17:21

    It's attempting to read the object before it casts it. Hence you're getting the null exception instead of a cast exception.

提交回复
热议问题