Do "type-safe" and "strongly typed" mean the same thing?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
No, not necessarily - although it depends on your definition of the terms, and there are no very clear and widely accepted definitions.
For instance, dynamic programming languages are often type safe, but not strongly typed. In other words, there's no compile-time type information determining what you can and can't do with a type, but at execution time the runtime makes sure you don't use one type as if it were another.
For example, in C# 4.0, you can do:
The last line is the key to it being type-safe: there's no safe conversion from an int array to a
FileStream
, so the operation fails - instead of treating the bytes of the array object as if they were aFileStream
.EDIT: C# is normally both "strongly typed" (as a language) and type safe: the compiler won't let you attempt to make arbitrary calls on an object, and the runtime won't let you perform inappropriate conversions.
I'm not entirely sure where unsafe code fits in - I don't know enough about it to comment, I'm afraid.
Dynamic typing in C# 4 allows weakly typed but still type-safe code, as shown above.
Note that
foreach
performs an implicit conversion, making it a sort of hybrid:This will compile (there was another question on this recently) but will fail at execution time. Ironically, that's because you're trying to be strongly typed with respect to the
stream
variable, which means you have to perform a cast on the result of the iterator."Type Safe" means that there's no casting involved and no run-time type errors can occur.
Some people argue that "Strongly Typed" mean nothing, or "it's good", or "i'm comfortable with it".
Anyway, "Type Safe" relates to a portion of code or API, when "Strongly Typed" refers to a whole language or platform.
ype safe means preventing programs from accessing memory outside the bounds of an object's public properties. When code is not type-safe, unwanted side effects can occur. Type-safety is important for assembly isolation and security enforcement. When code is type- safe, the common language runtime can completely isolate assemblies from each other
They are the basically same, it's just a matter of interpretation:
From wikipedia:
Good question. Read this wikipedia entry, here's an extract: