Pages

Thursday, 11 June 2015

Type and Storage

What is The Type?

A type defines the blueprint for a value, in the other way we can say that type is a template for creating data structures.It isn’t the data structure itself, but it specifies the characteristics of objects constructed from the template.

A type is defined by the following elements:
  • A name
  • A data structure to contain its data members
  • Behaviors and constraints


Image above show us the structure of int and short.

Data Members and Function Members

There are two kinds of members in a type: data members and function members.
  • Data members store data that is relevant to the object of the class or to the class as a whole.
  • Function members execute code. Function members define how the type can act.

Figure below shows you int member:

Type Classification

A type can be classified based on how they defined and how they are stored in memory. From the way a type defined, we can separate to two groups, pre-defined and user-defined type. From how a type stored in memory, we can separate them in to value type and reference type.
Predifined Types simple Non-Numeric bool
char
Numeric Integer 8-Bit sbyte
byte
16-Bit short
ushort
32-Bit int
uint
64-Bit long
ulong
Floating Point decimal
float
double
object
string
Dynamic
Userdefined Types class Type
struct types
array types
enum types
delegate types
interface types

Predifined Types

Table below show Simple Predifined Type
Name Meaning Range .NET Framework Type Default Value
sbyte 8-bit signed integer -128 – 127 System.SByte 0
byte 8-bit unsigned integer 0 – 255 System.Byte 0
short 16-bit signed integer -32,768 – 32,767 System.Int16 0
ushort 16-bit unsigned integer 0 – 65,535 System.UInt16 0
int 32-bit signed integer -2,147,483,648 – 2,147,483,647 System.Int32 0
uint 32-bit unsigned integer 0 – 4,294,967,295 System.UInt32 0
long 64-bit signed integer -9,223,372,036,854,775,808 – 9,223,372,036,854,775,807 System.Int64 0
ulong 64-bit unsigned integer 0 – 18,446,744,073,709,551,615 System.UInt64 0
float Single-precision float 1.5×10-45–3.4×1038 System.Single 0.0f
double Double-precision float 5×10-324–1.7×10308 System.Double 0.0d
decimal Decimal value with 28-significant-digit precision ±1.0×1028–±7.9×1028 System.Decimal 0m
bool Boolean true, false System.Boolean false
char Unicode character U+0000–U+ffff System.Char \x0000
char Unicode character U+0000–U+ffff System.Char \x0000

Table below show Non-Simple Predifined Type
Name Meaning .NET Framework Type
object The base class from which all other types, including the simple types are derived System.Object
string A sequence of zero or more Unicode characters System.String
dynamic A type designed to be used with assemblies written in dynamic languages No corresponding .NET type

Userdefined Types

Besides predifined types, we can also create our own-defined class. C# provides six kinds of custom defined class, as below:
  • class types
  • struct types
  • array types
  • enum types
  • delegate types
  • interface types
Different from predifined types, userdefined type you need to create a type declaration, which includes the following information:
  • The kind of type you are creating
  • The name of the new type
  • A declaration (name and specification) of each of the type’s members—except for array and delegate types, which don’t have named members
For example, figure below shows you how to work with user defined class.

Value Type and Reference Type

Stack and Heap

Stack is a list of memory that acts as a last-in, first-out (LIFO) data structure. It stores several types of data:
  • The values of certain types of variables
  • The program’s current execution environment
  • Parameters passed to methods

Heap is an area of memory where chunks are allocated to store certain kinds of data objects. Unlike the stack, data can be stored and removed from the heap in any order.
Although you can store data in heap, but you can't delete them explicitly. Instead, the Garbage Collector (GC) will do it for you. When your program doesn't access the object in memory, the GC will delete and free the memory. This will free us from memory leaking problem.

Value Types and Reference Types

Types are divided into two categories: value types and reference types. Objects of these types are stored differently in memory.
  • Value types require only a single segment of memory, which stores the actual data.
  • Reference types require two segments of memory:
    • The first contains the actual data—and is always located in the heap.
    • The second is a reference that points to where the data is stored in the heap.
Category Description
Value types Simple types Signed integral: sbyte, short, int, long
Unsigned integral: byte, ushort, uint, ulong
Unicode characters: char
IEEE flating point: float, double
High-precision decimal: decimal
Boolean: bool
Enum types User-defied types of the form enum E {...}
Struct types User-defied types of the form struct S {...}
Nullable types Nullable types Extensions of all other value types with a null value
Reference types Class types Ultimate base class of all other types: object
Unicode strings: string
User-defied types of the form class C {...}
Interface types User-defied types of the form interface I {...}
Array types Single- and multi-dimensional; for example, int[] and int[,]
Delegate types User-defied types of the form e.g. delegate int D(...)

0 comments:

Post a Comment