Why does a static constructor not have any parameters?

前端 未结 9 1530
忘掉有多难
忘掉有多难 2021-02-07 03:02

Per MSDN:

A static constructor does not take access modifiers or have parameters.

A static constructor is called automatically to ini

9条回答
  •  野性不改
    2021-02-07 03:21

    How would you control the arguments that were passed to such a constructor, given that it's invoked automatically by the run-time when the class is referenced for the first time, and can't be called directly?

    In theory, such a syntax could have been devised and implemented, but then that would necessitate its direct invocation, since now a simple class reference won't know what to pass in as arguments to it. The whole point of the static constructor is to perform type-level initializing prior to using the type. Doing so automatically ensures that this is the case, whereas direct invocation leaves plenty of room for mistakes.

提交回复
热议问题