Monday, 28 February 2011

Chapter 5 An Introduction to C# Data-Types

 
As we all know every programming language has Constants and Variables. These can be of different types. Basically there are two kinds of constants Numeric and Alpha-Numeric or String. Thus to store these values we need different type of variables. Obviously same two classification applies but under numeric they can be signed,unsigned,float,double etc. We call these classifications based on the values stored as data types. This chapter will look into the different data types in C#.

In C# Data Types are broadly categorized in two parts
  1. Value Types which store values, or
  2. Reference Types, which store references to the actual data.
We will discuss the second type at a later stage. Lets look at first type in detail in this chapter.

Value Types
These can be further divided in
  • Structs
  • Enumerations
Structs

Now we will see the most basic kind of datatypes under the Struct type
  1. Numeric types
    1. Integral types
    2. Floating-point types
    3. decimal
  2. bool
  3. User defined structs.
Features Of Value Types
  1. These variables work with values only therefore one when one variable is assigned to another the value is copied unlike the reference type where the reference is copied
  2. All value types are derived implicitly from the System.ValueType.Go to .NET framework type table
  3. We cannot create a new type by using a value type.
  4. Each type has a implicit constructor which initializes the variable with a default value.Go to default value table
  5. All the value types are aliases of .NET Framework System types. e.g int is the alias for System.Int32
  6. Unlike reference types, it is not possible for a value type to contain the null value.
Local variables in C# must be initialized before being used. Therefore, if you declare a local variable without initialization like this:

int myInt;
you cannot use it before you initialize it. You can initialize it using the following statement:

myInt = new int();  // Invoke default constructor for int type.
which is equivalent to:

myInt = 0;         // Assign an initial value, 0 in this example.
You can, of course, have the declaration and the initialization in the same statement like this:

int myInt = new int();

–or–

int myInt = 0;

Using the new operator calls the default constructor of the specific type and assigns the default value to the variable. In the preceding example, the default constructor assigned the value 0 to myInt.

With user-defined types, use new to invoke the default constructor. For example, the following statement invokes the default constructor of the Point struct:

Point p = new Point(); // Invoke default constructor for the struct.

After this call, the struct is considered to be definitely assigned; that is, all of its members are initialized to their default values.

Integral Types

The following table shows the sizes and ranges of the integral types, which constitute a subset of simple types.
Type Range Size
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
uint 0 to 4,294,967,295 Unsigned 32-bit integer
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer
ushort
0 to 65,535

Unsigned 16-bit integer
short -32,768 to 32,767 Signed 16-bit integer
char U+0000 to U+ffff Unicode 16-bit character
byte
0 to 255
Unsigned 8-bit integer
sbyte -128 to 127 Signed 8-bit integer
If the value represented by an integer literal exceeds the range of ulong, a compilation error will occur.

Floating-Point Types


Type Approximate range Precision
float ±1.5e−45 to ±3.4e38 7 digits
double ±5.0e−324 to ±1.7e308 15-16 digits


Decimal Type


The decimal keyword denotes a 128-bit data type. Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table.
Type Approximate range Precision
decimal ±1.0 × 10−28 to ±7.9 × 1028 28-29 significant digits

If you want a numeric real literal to be treated as decimal, use the suffix m or M, for example:
decimal xMoni = 10.4m;
Without the suffix m, the number is treated as a double, thus generating a compiler error.

Conversions

The integral types are implicitly converted to decimal and the result evaluates to decimal. Therefore you can initialize a decimal variable using an integer literal, without the suffix, as follows:
decimal xPaisa = 200;
There is no implicit conversion between floating-point types and the decimal type; therefore, a cast must be used to convert between these two types. For example:
decimal xPaisa = 199.9m;
double xd = (double)xPaisa;
xPaisa = (decimal)xd;
You can also mix decimal and numeric integral types in the same expression. However, mixing decimal and floating-point types without a cast results in a compilation error.

Default Values


Value type Default value
bool false
byte 0
char '\0'
decimal 0.0M
double 0.0D
enum The value produced by the expression (E)0, where E is the enum identifier.
float 0.0F
int 0
long 0L
sbyte 0
short 0
struct The value produced by setting all value-type fields to their default values and all reference-type fields to null.
uint 0
ulong 0
ushort 0



.NET Framework Types and C# Aliases


The following table shows the keywords for built-in C# types, which are aliases of predefined types in the System  namespace.
C# Type .NET Framework Type
bool System.Boolean
byte System.Byte
sbyte System.SByte
char System.Char
decimal System.Decimal
double System.Double
float System.Single
int System.Int32
uint System.UInt32
long System.Int64
ulong System.UInt64
object System.Object
short System.Int16
ushort System.UInt16
string System.String

No comments:

Post a Comment