Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is type-safe in .net?

Tags:

c#

.net

What is type-safe?

What does it mean and why is it important?

like image 766
Surya sasidhar Avatar asked Mar 13 '10 06:03

Surya sasidhar


People also ask

What is type-safe in C#?

C# is primarily a type-safe language, meaning that types can interact only through protocols they define, thereby ensuring each type's internal consistency. For instance, C# prevents you from interacting with a string type as though it were an integer type.

What means type-safe?

A language is type-safe if the only operations that can be performed on data in the language are those sanctioned by the type of the data. Java is not type-safe, though it was intended to be. A Java object may read and modify fields (and invoke methods) private to another object.

What is type-safe in typescript?

as allows us to cast any type to any other type without proving in our code that doing so is valid. If we cannot prove that a type conversion is valid, maybe it is not! any and as do have their places, but the less we use them, the more type-safe our code will be.

What is type-safe in Android?

Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable. Show activity on this post. Type safety means that compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.


2 Answers

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 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 though it's a string and not an integer, can legally be added to an ArrayList since String derives (like Int32) from Object. However, it will throw an InvalidCastException when you try to set integer to (int)integers[2] because a String cannot be cast to an Int32.

On the other hand, the List<T> class is type-safe for exactly the opposite reason--i.e., the above code would not compile if integers were a List<int>. Any value that you the developer access from within a type-safe List<int> you can be certain is an int (or whatever the corresponding T is for any generic List<T>); and you can therefore be sure that you'll be able to perform operations such as casting to int (obviously) or, say, long.

like image 165
Dan Tao Avatar answered Oct 14 '22 18:10

Dan Tao


C - You declare an int, cast it to char and access memory beyond int's boundary

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

like image 28
A9S6 Avatar answered Oct 14 '22 20:10

A9S6