Code with undefined behavior in C#

后端 未结 7 712
面向向阳花
面向向阳花 2020-12-03 06:47

In C++ there are a lot of ways that you can write code that compiles, but yields undefined behavior (Wikipedia). Is there something similar in C#? Can we write code in C# th

相关标签:
7条回答
  • 2020-12-03 07:06

    In general I would say no.

    Use Automatic variable before it’s initialized.

    All variables must be initialized. If not an exception occurs.

    Division by zero

    Exception is thrown.

    Indexing an array out of bounds

    Exception is thrown

    As Aequitarum Custos pointed out you can use unsafe code. Then again this isn't really C#, you are explicitly opting out of the C# environment.

    0 讨论(0)
提交回复
热议问题