What is the CLR implementation behind raising/generating a null reference exception?

前端 未结 3 744
旧时难觅i
旧时难觅i 2021-01-18 21:16

We do come across this particular and one of the most common exception in our coding/development life day or another day. My Question is NOT about W

3条回答
  •  孤街浪徒
    2021-01-18 22:04

    Have you read the CLI Spec - ECMA-335? You will find some answers there.

    11 Semantics of classes...When a variable or field that has a class as its type is created (for example, by calling a method that has a local variable of a class type), the value shall initially be null, a special value that := with all class types even though it is not an instance of any particular class.

    And the description of the ldnull instruction:

    The ldnull pushes a null reference (type O) on the stack. This is used to initialize locations before they become live or when they become dead. [Rationale: It might be thought that ldnull is redundant: why not use ldc.i4.0 or ldc.i8.0 instead? The answer is that ldnull provides a size-agnostic null – analogous to an ldc.i instruction, which does not exist. However, even if CIL were to include an ldc.i instruction it would still benefit verification algorithms to retain the ldnull instruction because it makes type tracking easier. end rationale] Verifiability: The ldnull instruction is always verifiable, and produces a value of the null type (§1.8.1.2) that is assignable-to (§I.8.7.3)any other reference type.

提交回复
热议问题