Why doesn't C# offer constness akin to C++?

前端 未结 5 1425
悲哀的现实
悲哀的现实 2021-02-07 12:53

References in C# are quite similar to those on C++, except that they are garbage collected.

Why is it then so difficult for the C# compiler to support the following:

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 13:23

    The question is, do we need constness in C#?

    1. I'm pretty sure that the JITter knows that the given method is not going to affect the object itself and performs corresponding optimizations automagically. (maybe by emitting call instead of callvirt ?)

    2. I'm not sure we need those, since most of the pros of constness are performance related, you end up at the point 1.

    Besides that, C# has the readonly keyword.

提交回复
热议问题