C# Data Types - NetwaxLab

Breaking

Facebook Popup

BANNER 728X90

Saturday, January 17, 2015

C# Data Types

Data Types are used everywhere in a programming language. It means, that you cannot use variable without data types. Data types are used to inform the compiler about what type value variable can store? This is a set of values and allowable operations on those values. Such as if you want to work with int value then you will have to assign int type variable to work with.

C-Sharp provides two types of data types
  1. Value Types.
  2. Reference Types.

  • Value Types

These data types contain the directly value. These types are created on the stack and derived from the class System.ValueType. Primitive types (Except strings), Enumeration, Structure are Value types.

Primitive Types

Bool: Bool is one of the simple data types. It can contain only 2 values – “false” or “true”. The default value of Bool is “false”.

Int: Int is short for integer, a data type for storing number without decimals. When working with numbers, int is the most commonly used data type. Integers have several data types within C# depending on the size of the number they are supposed to store.


Type
Type Map
Default
Size
Range
Sbyte
System.SByte
0
1 byte
-128 to 127
Byte
System.Byte
0
1 byte
0 to 255
Short
System.Int16
0
2 byte
-32,768 to 32,767
Ushort
System.UInt16
0
2 byte
0 to 65,535
Int
System.Int32
0
4 byte
-2,147,483,648 to 2,147,483,647
Uint
System.UInt32
0
4 byte
0 to 4,294,967,295
Long
System.Int64
0L
8 byte
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Ulong
System.UInt64
0
8 byte
0 to 18,446,744,073,709,551,615

Example: 1

Example Input
Example Output

Char: Char is used for storing a single character.

Float: Float is one of the data types used to store the decimal value.  In C# we have three floating point types: float, double, and decimal.


Type
Type Map
Default
Size
Range
Float
System.Single
0
4 byte
1.5 x 10-45 to 3.4 x 1038
Double
System.Double
0
8 byte
5.0 x 10-324 to 1.7 x 10308
Decimal
System.Decimal
0D
16 byte
1.0 x 10-28 to 7.9 x 1028

Example: 2

Example Input
Example Output

  • Reference Types

The Reference types do not contain the actual data stored in a variable, but they contain a reference to the variables. Reference types are created on the Heap. The lifetime of the reference type is managed by the .NET framework. The default value for reference types is null reference. Assignment to a variable of a reference type creates a copy of the reference rather than a copy of the referenced value. Classes, strings, interfaces, arrays, and delegates are reference types.

----

No comments:

Post a Comment