Can convert from one type to another implicitly and

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: set properties, etc. Can convert from one type to another Implicitly and explicitly Types are organized Namespaces There are two categories of types: value and reference Types are arranged in a hierarchy Types Unified Type System Value types Directly contain data Cannot be null Reference types Contain references to objects May be null int i = 123; string s = "Hello world"; i s 123 "Hello world" Types Unified Type System Value types Primitives Enums Structs int i; float x; enum State { Off, On } struct Point {int x,y;} Reference types Root String Classes Interfaces Arrays Delegates object string class Foo: Bar, IFoo {...} interface IFoo: IBar {...} string a = new string[10]; delegate void Empty(); Types Unified Type System Value (Struct) Reference (Class) Variable holds Actual value Memory location Allocated on Stack, member Heap Always has value May be null 0 null Aliasing (in a scope) No Yes Assignment means Copy data Copy reference Nullability Default value Types Unified Type System Benefits of value types No heap allocation, less GC pressure More efficient use of memory Less reference indirection Unified type system No primitive/object dichotomy Types Conversions Implicit conversions Occur automatically Guaranteed to succeed No information (precision) loss Explicit conversions Require a cast May not succeed Information (precision) might be lost Both implicit and explicit conversions can be user-defined Types Conversions int x = 123456; long y = x; short z = (short)x; // implicit // explicit double d = 1.2345678901234; float f = (float)d; long l = (long)d; // explicit // explicit Types Unified Type System Everything is an object All types ultimately inherit from object Any piece of data can be stored, transported, and manipulated with no extra work object Stream MemoryStream Hashtable FileStream int double Types Unified Type System Polymorphism The ability to perform an operation on an object without knowing the precise type of the object void Poly(object o) { Console.WriteLine(o.ToString()); } Poly(42); Poly(“abcd”); Poly(12.345678901234m); Poly(new Point(23,45)); Types Unified Type System Question: How can we treat value and reference types polymorphically? How does an int (value type) get converted into an object (reference type)? Answer: Boxing! Only value types get boxed Reference types do not get boxed Types Unified Type System Boxing Copies a value type into a reference type (object) Each value type has corresponding “hidden” reference type Note that a reference-type copy is made of the value type Value types are never aliased Value type is converted implicitly to object, a reference type Essentially an “up cast” Types Unified Type System Unboxing Inverse operation of boxing Copies the value out of the box Copies from reference type to value type Requires an explicit conversion May not succeed (like all explicit conversions) Essentially a “down cast” Types Unified Type System Boxing and unboxing int i = 123; i object o = i; o int j = (int)o; 123 Syste...
View Full Document

This note was uploaded on 04/04/2014 for the course CS 5950 taught by Professor Staff during the Summer '08 term at Western Michigan.

Ask a homework question - tutors are online