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];
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 .
Dan Tao Mar 13 '10 at 6:21 2010-03-13 06:21
source share