Data
Types: it is a type in a programming
language that a variable can hold . C# is a strongly typed
language, so we define data type for
variables. We cannot use variable
without data types.
When
we declare a variable, we have to tell the compiler about what type of the data
the variable can hold or which data type the variable belongs to.
Syntax
: DataType VariableName
DataType : The type of data that the variable
can hold
VariableName : the variable we declare for
hold the values.
The
varibles in C#, are categorized into the following types:
- Value types
- Reference types
- Pointer types
(a)Value Types:
If a variable holds actual value then the type
of data types are value types. Value type variables can be assigned a value
directly. They are derived from the class System.ValueType. These value
types are stored in “stack” memory and these value types are fixed in size. If
you assign a value of a variable to another variable it will create two copies.
Ex: byte,
short, int, float, double, long ,char, bool, DateTime.
All
primitive data type except string and object are example of value types.
Object
is a super type. It can store any type and any size of data. Object is called
super type because it helps in inheritance.
struct
and enum are value type.
CSharp
has a set of predefined value types.
Data
Type
|
Description
|
Nanespace Class
|
Range
|
Bool
|
Boolean value
|
System.Boolean
|
True or False
|
Byte
|
8-bit unsigned integer
|
System.Byte
|
0 to 255
|
Char
|
16-bit Unicode character
|
System.Char
|
U +0000 to U +ffff
|
Decimal
|
128-bit precise decimal values with 28-29 significant digits
|
System.Decimal
|
(-7.9 x 1028 to 7.9 x 1028) / 100 to 28
|
Double
|
64-bit double-precision floating point type
|
System.Double
|
(+/-)5.0 x 10-324 to (+/-)1.7 x 10308
|
Float
|
32-bit single-precision floating point type
|
System.Single
|
-3.4 x 1038 to + 3.4 x 1038
|
Int
|
32-bit signed integer type
|
System.Int32
|
-2,147,483,648 to 2,147,483,647
|
Long
|
64-bit signed integer type
|
System.Int64
|
-923,372,036,854,775,808 to 9,223,372,036,854,775,807
|
Sbyte
|
8-bit signed integer type
|
System.Sbyte
|
-128 to 127
|
Short
|
16-bit signed integer type
|
System.Int16
|
-32,768 to 32,767
|
Uint
|
32-bit unsigned integer type
|
System.UInt32
|
0 to 4,294,967,295
|
Ulong
|
64-bit unsigned integer type
|
System.UInt64
|
0 to 18,446,744,073,709,551,615
|
Ushort
|
16-bit unsigned integer type
|
System.UInt16
|
0 to 65,535
|
(b)Reference types
A
variable holds the address of value
instead of actual value stored in memory . If multiple
variables reffering to a memory
location. If the data in the memory location is changed by one of the
variables, the other variable automatically reflects this change in value
.These reference types are stored in “heap” memory . Reference types are not
fixed in size. They are maintained in system managed heap but it also
uses stack to store reference of the heap Two primitive types (string and
object) and non-primitive data types (class, interface & delegate) are
examples of reference type. Example of
reference types are: object, dynamic, and string.
Object Type
The
Object Type is derived from System.Object class. The object types can be
assigned values of any other types, value types, reference types, predefined or
user-defined types. However, before assigning values, it needs type conversion.
When
a value type is converted to object type, it is called boxing and on the
other hand, when an object type is converted to a value type, it is called unboxing.
object
obj;
obj
= 100; // this is boxing
Dynamic Type
We
can assign any type of value in the dynamic data type variable. Type
checking of variables takes place at
run-time.
Syntax
for declaring a dynamic type is:
dynamic
<variable_name> = value;
For
example,
dynamic
d = 100;
Dynamic
types are similar to object types but the only difference is that type checking
for object type variables takes place at compile time, whereas that for the
dynamic type variables takes place at run time.
String Type
The
String Type allows you to assign any string values to a variable. The
string type is an alias for the System.String class. It is derived from object
type. The value for a string type can be assigned using string literals in two
forms: quoted and @quoted.
For
example,
String
str = "Suhail Osmani";
The
user-defined reference types are: class, interface, or delegate. We will
discuss these types in later chapter.
(a)Pointer
Type
Pointer
type variables store the memory address of another type. Pointers in C# have
the same capabilities as the pointers in C or C++.
Syntax
:
type*
identifier;
Example,
char*
mhb;
int*
hjv;
fgre
ReplyDelete