9

我最近参加了一次脑力测验,得到了高分,但有几个问题对我来说很难。也许是因为英语不是我的母语......问题之一是:

以下哪一项描述了类型安全?

  1. 用于确保 CLR 中引用和值类型的安全性的编程构造
  2. 防止因禁止非托管访问而导致的内存泄漏
  3. 特定于 CLR 的功能保证类型不能访问它们自己的 AppDomain 之外的内存
  4. 一种通过使用强名称键来保护程序集及其类型的机制
  5. 处理保证分配的对象总是以兼容的方式访问的概念

我认为是 1 或 5,但它们对我来说听起来很奇怪:(

你怎么看?

4

7 回答 7

10

实际上我认为这是选择 5,因为类型安全与安全性无关。

于 2009-05-29T21:46:23.893 回答
7

Type Safety is the feature of a language designed to make good on [Robin Milner][1]'s famous slogan about ML programming: well-typed programs cannot go wrong.

The slogan needs some unpacking before it can be properly understood, but it basically means that programs cannot fail because of a runtime type error, i.e. when the parameters applied to constructor or a function have values of incompatible type.

Consider a language that allows integers, integer functions as first class values, function abstraction and partial function application, and which defines the usual integer arithmetic operators as binary functions. The property of type safety is what the compiler enforces to ensure that both of the arguments to the addition operator are expressions that reduce to integers and not to functions. If a program is well-typed, then the compiler can emit an executable object for it. Otherwise, it flags the programming error and aborts.

于 2009-05-29T22:09:29.213 回答
4

According to the msdn link provided below, http://msdn.microsoft.com/en-us/library/hbzz1a9a.aspx

Type-safe code accesses only the memory locations it is authorized to access. (For this discussion, type safety specifically refers to memory type safety and should not be confused with type safety in a broader respect.) For example, type-safe code cannot read values from another object's private fields. It accesses types only in well-defined, allowable ways.

于 2013-03-06T20:32:46.833 回答
1

选项 5 处理保证分配的对象总是以兼容的方式访问的概念

类型安全涉及确保在创建 Foo 时不能将其视为 Bar。如果您不知道它是什么类型(或无法保证),那么您编写的代码可能无法按预期工作。

于 2009-05-29T21:49:37.867 回答
1

类型安全语言将确保(在编译时)您不会在类型上调用不兼容的方法,例如在 int 类型上调用 length()。非类型安全语言会在运行时解决。所以,选择5。

于 2009-05-29T21:49:58.990 回答
1

As other have said, choice 5...

In general - for .NET, check out the Common Type System (CTS) which enables cross-language stuff and type safety.

Check out: http://en.wikipedia.org/wiki/Type_safety ...

于 2009-05-29T22:45:57.597 回答
0

It is option #5. Type safety is an assurance, not a concrete thing. It is possible for .NET code to not be type safe...say in the case that an assembly uses unsafe code to perform unmanaged calls (PInvoke). During JIT, a process is performed that verifies the types being jitted are, indeed, type safe. I am not aware of any particulars about this process, but if a jitted type passes, then it is considerd verifiably type safe.

于 2009-05-29T21:51:05.007 回答