#952 – Type Safety

C# enforces type safety.  Type safety is the idea that you can only interact with an object in ways that are allowed by the object’s type.  You can’t treat an instance  of one type like it is actually an instance of another type.

Below is an example of some C++ code that is not type safe.  We use a pointer to treat a floating point value as an integer.  This results in an unexpected integer value.  This behavior is not normally something that you cannot do in C# (without marking the code as “unsafe”).

	double d1 = 1.1;

	void* ptr = &d1;    // Pointer to memory where double is stored
	int* ptr2 = (int*)ptr;   // Use the pointer as a pointer to int

	int n1 = *ptr2;     // Treat double as int



About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #952 – Type Safety

  1. Pingback: Dew Drop – October 15, 2013 (#1,645) | Morning Dew

  2. Pingback: Free ebook Introducing Windows 8.1 In The Daily Six pack: Oct 16, 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: