.NET Types and PerlNET

The .NET types that we use in PerlNET may be divided into two major groups: value types and reference types. Value type variables store the value itself, while reference type variables store a reference (an address in memory where the data is located). References are useful for compound types, and usually we use them to work with instances of .NET class types—objects.

Whenever a .NET method returns an object, PerlNET stores a reference to it unless this object represents one of the value types: System.Int32, System.Double, System.Decimal, and so on. Here are the .NET value types of our interest (they all reside in the System namespace):

  • Boolean

  • Char

  • SByte

  • Int16

  • Int32

  • Int64

  • Byte

  • UInt16

  • UInt32

  • UInt64

  • Single

  • Double

  • Decimal

PerlNET stores value types as boxed objects, maintaining their .NET type information, instead of converting them into regular Perl scalars. Such boxed objects may be a result of either storing the return value of some .NET method (in case the method returns one of the above mentioned value types) or constructing a value type object using special cast functions defined in the PerlNET module.

When we call .NET methods and pass Perl scalars, the latter are converted into corresponding .NET objects by PerlNET runtime modules and are passed to .NET. Some scalars must be cast to the correct value type as expected by the .NET method.[1]

[1] Perl strings are scalars and PerlNET automatically converts them into the System.String class objects. We do not need to perform any casting operation before passing string scalars to .NET methods.

We explain the process of casting, marshaling, and working with value types later in this chapter when we discuss calling methods. We will demonstrate this with concrete examples.

System.Object

.NET is a strong-typed object-oriented environment. Every .NET type has a class associated with it. This includes even value types: for example, the System.Int32 class represents int type and the System.Double represents the double type. Every class in .NET directly or indirectly inherits from the same root class: System.Object (inheritance in .NET and its implementation in PerlNET is discussed in Chapter 12). This class defines several methods, such as ToString and Equals. Every .NET class overrides or inherits as is the System.Object methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.199.162