Code with undefined behavior in C#

后端 未结 7 747
面向向阳花
面向向阳花 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 06:51

    As others have mentioned, pretty much anything in the "unsafe" block can yield implementation-defined behaviour; abuse of unsafe blocks allows you to change the bytes of code that make up the runtime itself, and therefore all bets are off.

    The division int.MinValue/-1 has an implementation-defined behaviour.

    Throwing an exception and never catching it causes implementation-defined behaviour -- terminate the process, start a debugger, and so on.

    There are a number of other situations in C# where we are forced to emit code which has implementation-determined behaviour. For example, this situation:

    http://blogs.msdn.com/ericlippert/archive/2006/04/06/odious-ambiguous-overloads-part-two.aspx

    However, the situations in which a safe, well-behaved C# program has implementation-defined behaviour should be quite rare.

提交回复
热议问题