Answered by:
what is type safety ,type safety in c#

Question
-
What is type casting ,type safety in C# . Plz provide me the example for that.Friday, October 8, 2010 6:43 AM
Answers
-
Maybe to simple but...
http://en.csharp-online.net/Typecasting
http://www.codeguru.com/columns/DotNet/article.php/c9661/
Hope this helps
Maurice
Maurice- Marked as answer by Rubel Khan Thursday, October 21, 2010 3:28 PM
Friday, October 8, 2010 12:10 PM -
Hi,
It seems that you're new to .Net. If you don't recall details in code that I'll expose feel free to post me back
for the casting there are two types explicit and implicit casting
implicit casting:
Imagine you declare a double x, you can write
double x = 2; recall that 2 is integer but I can stock it in the x variable
explicit casting:
class Mother
{
}
class Daugther: Mother
{
}Mother m = new Daugther();
Daugther d = (Daugther) m;this is a explicit cast that use the () to indicate to which type you want to convert your object
or
Mother m = new Daugther();
Daugther d = m as Daugther ;//recall that as operator is only used with reference types and not values types to see what's happen if there are value types just change the class by struct for Daughter and MotherOf Corse , for the type safty it is the case when you do not specify the right type for the conversion so that it leads to an invalidcast exception or at least some data precision is lost if you convert form int to short
int x = 7000;
short y = (short)x;
The complexity resides in the simplicity- Marked as answer by Rubel Khan Thursday, October 21, 2010 3:28 PM
Thursday, October 14, 2010 8:30 PM
All replies
-
Maybe to simple but...
http://en.csharp-online.net/Typecasting
http://www.codeguru.com/columns/DotNet/article.php/c9661/
Hope this helps
Maurice
Maurice- Marked as answer by Rubel Khan Thursday, October 21, 2010 3:28 PM
Friday, October 8, 2010 12:10 PM -
Hi,
It seems that you're new to .Net. If you don't recall details in code that I'll expose feel free to post me back
for the casting there are two types explicit and implicit casting
implicit casting:
Imagine you declare a double x, you can write
double x = 2; recall that 2 is integer but I can stock it in the x variable
explicit casting:
class Mother
{
}
class Daugther: Mother
{
}Mother m = new Daugther();
Daugther d = (Daugther) m;this is a explicit cast that use the () to indicate to which type you want to convert your object
or
Mother m = new Daugther();
Daugther d = m as Daugther ;//recall that as operator is only used with reference types and not values types to see what's happen if there are value types just change the class by struct for Daughter and MotherOf Corse , for the type safty it is the case when you do not specify the right type for the conversion so that it leads to an invalidcast exception or at least some data precision is lost if you convert form int to short
int x = 7000;
short y = (short)x;
The complexity resides in the simplicity- Marked as answer by Rubel Khan Thursday, October 21, 2010 3:28 PM
Thursday, October 14, 2010 8:30 PM