Page 37 - DCAP312_WEB_TECHNOLOGIES_II
P. 37

Unit 2: Introduction to C#



            the conversion rules below. The following illustrates the cross-language compatibility of types   Notes
            by comparing C# code with the equivalent Visual Basic .NET code:

                        // C#
                  public void UsingCSharpTypeAlias()
                  {
                  int i = 42;
                  }
                  public void EquivalentCodeWithoutAlias()
                  {
                  System.Int32 i = 42;
                  }
                  //Visual Basic .NET
                  Public Sub UsingVisualBasicTypeAlias ()
                      Dim i as Integer = 42
                  End Sub
                  Public Sub EquivalentCodeWithoutAlias()
                      Dim i As System.Int32 = 42
                  End Sub
            Using the language specific type aliases is often considered more readable than using the fully-
            qualified .NET Framework type names.

            The fact that each C# type corresponds to a type in the unified type system gives each value type
            a consistent size across platforms and compilers. That consistency is an important distinction from
            other languages such as C, where, e.g. a long is only guaranteed to be at least as large as an Int,
            and is implemented with different sizes by different compilers. As reference types, variables of
            types derived from object (i.e. any class) are exempt from the consistent size requirement. That
            is, the size of reference types like System.IntPtr, as opposed to value types like System.Int, may
            vary by platform. Fortunately, there is rarely a need to know the actual size of a reference type.

            There are two predefined reference types: Object, an stage name for the System.Object class,
            from which all previous reference types derive; and string, an alias for the System.String class.
            C# likewise has several integral value types, each an alias to a corresponding value type in the
            System namespace of the .NET Framework. The predefined C# type aliases expose the methods
            of the underlying .NET Framework types. For example, since the .NET Framework’s System.
            Int32  type  implements  a  ToString()  method  to  convert  the  value  of  an  integer  to  its  string
            representation, C#’s int type exposes that method:
                          int i = 97;
                              string s = i.ToString();
                              // The value of s is now the string “97”.
            Likewise, the System.Int32 type implements the Parse() method, which can therefore be accessed
            via C#’s int type:
                           string s = “97”;
                           int i = int.Parse(s);
                        // The value of i is now the integer 97.






                                             LOVELY PROFESSIONAL UNIVERSITY                                    31
   32   33   34   35   36   37   38   39   40   41   42