Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Sunday, September 18, 2016

Lesson 2 - Built-in Types in C#

C# provides a standard set of built-in numeric types to represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. There are also built-in string and object types.

You can see this tutorials online also.
Part 1
Part 2


Type
Range
Size
-128 to 127
Signed 8-bit integer
0 to 255
Unsigned 8-bit integer
U+0000 to U+ffff
Unicode 16-bit character
-32,768 to 32,767
Signed 16-bit integer
0 to 65,535
Unsigned 16-bit integer
-2,147,483,648 to 2,147,483,647
Signed 32-bit integer
0 to 4,294,967,295
Unsigned 32-bit integer
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Signed 64-bit integer
0 to 18,446,744,073,709,551,615
Unsigned 64-bit integer

BOOL
The bool keyword is an alias of System.Boolean.
 It is used to declare variables to store the Boolean values, true and false.

Bool is often used in expressions. A bool can be assigned to the true and false literals. We set the bool variable to true. And then we invert the value of the bool using the exclamation operator.True, False


BYTE
The byte keyword denotes an integral type that stores values as indicated in the following table.
Type
Range
Size
.NET Framework type
byte
0 to 255
Unsigned 8-bit integer
We use byte if we know that the range is in between 0-255.
You can declare and initialize a byte variable like this example:
 byte age=45;
Byte  noOfMobiles=4;(camelcase rule)
 
One more thing I want to cover about byte is
byte x = 10, y = 20;
byte z= x+y;
it will give compilation error
// Error: conversion from int to byte:
 The
following assignment statement will produce a compilation error, because the
arithmetic expression on the right-hand side of the assignment operator
evaluates to int by default.

To fix this problem, use a cast: I will cover cast in upcoming videos.
// OK: explicit conversion:
byte z = (byte)(x + y);
INT
The int keyword denotes an integral type that stores values according to the size and range
shown in the following table.
Type
Range
Size
.NET Framework type
Default Value
int
-2,147,483,648 to 2,147,483,647
Signed 32-bit integer
0
We use int if we know that the range is in between -2,147,483,648-2,147,483,648.
You can declare and initialize a int  variable like this example:
 Int population =1000000;
int  No_employee=5000; To display the actual type for any C# type, use the system method GetType(). For example, the following statement displays the system alias that represents the type of myVariable: Console.WriteLine(myVariable.GetType());  FLOAT The float keyword signifies a simple type that stores 32-bit floating-point values. The following table shows the precision and approximate range for the float type. Precision means it gives accurate value to 7 digits.
Type
Approximate range
Precision
.NET Framework type
float
-3.4 × 1038to +3.4 × 1038
7 digits
By default, a real numeric literal on the right side of the assignment operator is treated as double. Therefore, to initialize a float variable, use the suffix f or F, as in the following example:
float x = 3.5F;
If you do not use the suffix in the previous declaration, you will get a compilation error because you are trying to store a double value into a float variable.
Double
The double keyword signifies a simple type that stores 64-bit floating-point values. The following table shows the precision and approximate range for the double type.
Precision means it gives accurate value to 15-16 digits
Type
Approximate range
Precision
.NET Framework type
double
±5.0 × 10−324 to ±1.7 × 10308
15-16 digits
Literals By default, a real numeric literal on the right side of the assignment operator is treated as double. However, if you want an integer number to be treated as double, use the suffix d or D, for example:
double x = 3D;
Decimal
The decimal keyword indicates a 128-bit data type. Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.
You should use decimalwhenever the accuracy is important.
The approximate range and precision for the decimal type are shown in the following table.
Precision means it gives accurate value to 28-29 digits.
Type
Approximate Range
Precision
.NET Framework type
decimal
(-7.9 x 1028 to 7.9 x 1028) / (100 to 28)
28-29 significant digits
Literals If you want a numeric real literal to be treated as decimal, use the suffix m or M, for example:
decimal myMoney = 300.5m;
Without the suffix m, the number is treated as a double and generates a compiler error.
When To Use Decimal, Double, and Float Decimal
In case of financial applications it is better to use Decimal types because it gives you a high level of accuracy and easy tavoid rounding errors
Double
Double Types are probably the most normally used data type for real values, except handling money.
Float
It is used mostly in graphic libraries because very high demands for processing powers, also used situations that can endure rounding errors.

1 comment :