none
what is type safety ,type safety in c# RRS feed

Answers

  • 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 Mother

    Of 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
    Thursday, October 14, 2010 8:30 PM

All replies

  • 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 Mother

    Of 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
    Thursday, October 14, 2010 8:30 PM