Unit-2.2 C# Language Datatype
Unit-2.2 C# Language Datatype
• Value types
• Primitives int i; float x;
• Enums enum State { Off, On }
• Structs struct Point {int x,y;}
• Reference types
• Root object
• String string
• Classes class Foo : IFoo {...}
• Interfaces interface IFoo {...}
• Arrays string[] a = new string[10];
• Delegates delegate void Empty();
Types
Each implicit data type in C# has its
corresponding .Net type.
Types
Each implicit data type in C# has its
corresponding .Net type.
Types
Unified Type System
• Implicit data types are represented in language using keywords, so
each of the above is a keyword in C# (Keyword are the words defined
by the language and can not be used as identifiers). It is worth noting
that string is also an implicit data type in C#, so string is a keyword in
C#. The last point about implicit data types is that they are value
types and thus stored on the stack, while user defined types or
referenced types are stored using the heap.
• A stack is a data structure that store items in a first in first out (FIFO)
fashion. It is an area of memory supported by the processor and its
size is determined at the compile time.
• A heap consists of memory available to the program at run time.
Reference types are allocated using memory available from the heap
dynamically (during the execution of program). The garbage collector
searches for non-referenced data in heap during the execution of
program and returns that space to Operating System.
Types
Unified Type System
Assignment
Copy data Copy reference
means
Types
Unified Type System
10
sp
20
cp CPoint
10
20
Types
Unified Type System
• Benefits of value types
• No heap allocation
• less GC pressure
• More efficient use of memory
• Less reference indirection
Types
Conversions
• Implicit conversions
• Occur automatically
• Guaranteed to succeed
• No information (precision) loss (e.g. short to long)
• Explicit conversions
• Require a cast
• May not succeed
• Information might be lost (e.g. long to short)
• Can define both implicit and explicit conversions
Types
Conversions
Examples
int x = 123456;
long y = x; // implicit
short z = (short)x; // explicit
double d = 1.2345678901234;
float f = (float)d; // explicit
long l = (long)d; // explicit
Types
Unified Type System
• Boxing
• Copies a value type into a reference type (object)
• Each value type has corresponding “hidden” reference type that is
defined automatically
• Note that a reference-type copy is made of
the value type
• Value types are never aliased
• The 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 (object) 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
object o = i; o System.Int32
123
int j = (int)o; j 123
Types
Unified Type System
• Benefits of boxing
• Enables polymorphism across all types
• Collection classes work with all types
Types
Unified Type System
• Disadvantages of boxing
• Performance cost
• The need for boxing will decrease when the CLR supports
generics (similar to C++ templates)