C# .Net Data Types
Data type Type of data Data type specifies the size of the data and type of data. C#.net data type is divided into two types
Value Type Int i = 0 I 10 (7000) C language C#
Reference Type Int * p = i p 7000 (9000) Stack Heap 90% 10% 10% 90%
The value type will be created at compile-time and these will be started in STACK memory. Value types hold the data directly No default value for value type GC (Garbage collector) cannot access stack memory. that is GC can not clear stack run time Examples
Pre-defined data types
Reference Type will be created at run time and these will be stored in HEAP memory. Reference Type holds the reference but not the data. The reference Type holds the default value. GC can access HEAP memory. Examples:-
Var (newly introduced)
Working with Predefined Data types
These are divided into 4 groups
Group 1:- Integral Data type
.byte 1 byte 0 255
.S byte 1 byte -128 to +127
.Short 2 bytes -32768 to +32768
.U short 2 bytes 0 – 65535
Int 4 bytes ~ +- 214 cores
U int 4bytes 0 -~ 429 cores
Long 8 bytes + 2^63
U Long 8 bytes 0 – 2 ^ 64 -1
Group 2:- Floating Related Data Types
*float 4 B
*double 8 B
*decimal 16 B
Group 3:- character Related Data types
.char 2 B: char holds UNICODE (universal coding) characters that are required for globalization.
.String 16 B : 4 GB
Group 4 :- other Data type
.bool 1 B true/ false
.Data time 12 B
1/1/0001 to 31/12/9999
Syntax to declare a variable
<Data type> var name = value;
Note:- In C# .net always variable name need to be started either with alphabet, underscore, @ symbol
Int a =90;
Int – a = 90;
Int @a = 90;
Int *a = 90;
Int 2a = 90;
Int if = 90;
Example on Data Types
Open console Application project.
Write the following code in the main method.
Int I = 90;
Console. Write Line (i);
Float x = 4.0 f Console. Write Line (x);
Byte b1 = 10, b2 = 20;
Int c = b1 + b2;
Console. Write Line (c);
Console. Write Line ();
Execute the project :(f5)
value types must be initialized before using
float X = 4.0 gives an error
By default 4.0 will be treated as double;
float x = 4. 0 F (or) float X = 4. 0f;
A number, which is post fixed with f @ F is called as float.
A number, which is post fixed with d @ D is called as double.
A number, which is postfixed with m@ M is called decimal.
Byte c = b1 + b2, gives an error + operator returns the result in the format of int.