What is type-safe?
What does it mean and why is it important?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re asking what the idea of “type-safe” in general means, it’s the characteristic of code that allows the developer to be certain that a value or object will exhibit certain properties (i.e., be of a certain type) so that he/she can use it in a specific way without fear of unexpected or undefined behavior.
For instance, in C#, you could say the
ArrayListclass is not type-safe because it can store any object, which means you can do something like the following:The above will compile because the value “3”, even though it’s a string and not an integer, can legally be added to an
ArrayListsinceStringderives (likeInt32) fromObject. However, it will throw anInvalidCastExceptionwhen you try to setintegerto(int)integers[2]because aStringcannot be cast to anInt32.On the other hand, the
List<T>class is type-safe for exactly the opposite reason–i.e., the above code would not compile ifintegerswere aList<int>. Any value that you the developer access from within a type-safeList<int>you can be certain is anint(or whatever the correspondingTis for any genericList<T>); and you can therefore be sure that you’ll be able to perform operations such as casting toint(obviously) or, say,long.