What is the safe type in .net?

What is a safe type?

What does it mean and why is it important?

+46
c #
Mar 13 '10 at 6:09
source share
6 answers

If you ask what the term “safe type” means in general, this is a characteristic of the code that allows the developer to be sure that the value or object will have certain properties (that is, be of a certain type) so that he / she can use it in a certain way, without fear of unexpected or undefined behavior.

For example, in C #, you can say that the ArrayList class is not type safe because it can store any object , which means you can do something like the following:

 var integers = new ArrayList(); integers.Add(1); integers.Add(2); integers.Add("3"); for (int i = 0; i < integers.Count; ++i) { int integer = (int)integers[i]; // do something } 

The above will compile because the value "3", even if it is a string and not an integer, can be legally added to an ArrayList , since String outputs (for example, Int32 ) from Object . However, if you try to set the integer to (int)integers[2] it will throw an InvalidCastException , because String cannot be attributed to Int32 .

On the other hand, the List<T> class is type safe for the exact opposite reason, i.e. the code above is not if integers was List<int> . Any value that you receive from the developer in a safe type List<int> can be defined - it is int (or any other T for any general List<T> ); and you can be sure that you can perform operations such as casting to int (obviously) or, say, long .

+60
Mar 13 '10 at 6:21
source share

C - You declare an int, pass it to char and access memory beyond the bounds

 int i = 10; char *s = (char*)i; print(*(s+10)); 

C # - Types are safe

 int i = 10; char *s //This is invalid unless you are using unsafe context. 

Pointers are not directly supported by .NET.

+12
Mar 13 '10 at 6:20
source share

Type-safe code provides access only to memory, access is allowed. For example, safe code cannot read values ​​from other objects. It addresses types only in well-defined, valid ways.

At just-in-time (JIT) compilation, an additional verification process parses the metadata and the Microsoft Intermediate Language (MSIL) method, which must be compiled by JIT into native machine code to make sure they are safe type. This process is skipped if the code has permission to bypass verification

Although type checking is not required to run managed code, type security plays a crucial role in isolation and assembly enforcement. When code is type safe, a common runtime can completely isolate assemblies from each other. This isolation helps ensure that assemblies cannot adversely affect each other, and this improves application reliability.

See the msdn link for more details.

Good article explaining it here

+10
Mar 13 '10 at 6:11
source share

Did you mean type-safe in particular or type-safety in general?

I do not agree with the accepted answer: ArrayList is type-safe (not type-safe and not type- specific ): https://stackoverflow.com/a/4168778

Richter - CLR via C #, 4th edition (p. 93):

Type-security is a core feature of the CLR. You can always detect an object type by calling the non-virtual method GetType System.Object.

For example, the Hero class cannot override the GetType method to become a SuperHero type.

This System.Object function allows the CLR to check whether objects can be created at run time. For example:

 internal class Employee { ... } public sealed class Program { public static Main() { DateTime dt = new DateTime(2016, 1, 1); PromoteEmployee(newYears); } public static PromoteEmployee(Object o) { Employee e = (Employee)o; //InvalidCastException, runtime error } } 

Casting a DateTime to an Employee is an example of an unsafe creation attempt.

0
Feb 02 '16 at 23:04
source share

Type safety has been introduced in .NET so that objects of one type do not look into the memory assigned to another object.

For example, and a detailed discussion, follow this link

-one
Aug 10 '15 at 6:17
source share

I do not agree with some answers here. C # has several levels of security .

EDIT: The security type has two levels of meaning (if we discuss programming languages ​​at all, as in this thread)

One of them is the time type of the compiled type, next to refactoring, etc., typos of the compiler’s catch, erroneous assignment of values ​​to incorrect variables (properties), i.e. string for int variable. Typical C # code is type safe, a well-known way to disable this function is the dynamic keyword or non-generic containers, errors like the ones mentioned above are delayed at runtime. Example: non-hacking C / C ++ code is (usually) safe at compile time. I think it is possible to write (crack) casting in C # that hide type conflicts.

The next level is the type of security at run time, C # is generally safe (without unsafe partitions). Even a dynamic value is checked at runtime. On the contrary: C / C ++ is not safe at runtime. If the compiler accepts the code, the odd assignment is not checked at runtime, providing a strange / strange / system level or late errors specific to C.

Few of the respondents in this thread mix up other areas where C # is safe (memory security, range security, null pointer, etc.). To be strict, these are different types of security.

Theory: https://en.wikipedia.org/wiki/Type_safety

-2
Sep 25 '15 at 6:37
source share



All Articles