What is a data types

C# - Data Types

C# is a strongly-typed language. It means we must declare the type of a variable which indicates the kind of values it is going to store such as integer, float, decimal, text, etc.
The following declares and initialized variables of different data types.
  1. string stringVar = "Hello World!!";
    int intVar = 100;
    float floatVar = 10.2f;
    char charVar = 'A';
    bool boolVar = true;

C# mainly categorized data types in two types: Value types and Reference types. Value types include simple types (e.g. int, float, bool, and char), enum types, struct types, and Nullable value types. Reference types include class types, interface types, delegate types, and array types. Learn about value types and reference types in detail in the next chapter.

C# Data Types

Predefined Data Types in C#

C# includes some predefined value types and reference types. The following table lists predefined data types:
TypeDescriptionRangeSuffix
byte8-bit unsigned integer0 to 255
sbyte8-bit signed integer-128 to 127
short16-bit signed integer-32,768 to 32,767
ushort16-bit unsigned integer0 to 65,535
int32-bit signed integer-2,147,483,648
to
2,147,483,647
uint32-bit unsigned integer0 to 4,294,967,295u
long64-bit signed integer-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
l
ulong64-bit unsigned integer0 to 18,446,744,073,709,551,615ul
float32-bit Single-precision floating-point type-3.402823e38 to 3.402823e38f
double64-bit double-precision floating-point type-1.79769313486232e308 to 1.79769313486232e308d
decimal128-bit decimal type for financial and monetary calculations(+ or -)1.0 x 10e-28
to
7.9 x 10e28
m
char16-bit single Unicode characterAny valid character, e.g. a,*, \x0058 (hex), or\u0058 (Unicode)
bool8-bit logical true/false valueTrue or False
objectBase type of all other types.
stringA sequence of Unicode characters
DateTimeRepresents date and time0:00:00am 1/1/01
to
11:59:59pm 12/31/9999
As you can see in the above table that each data types (except string and object) include value range. Compiler will give an error if value goes out of datatype's permitted range. For example, int data type's range is -2,147,483,648 to 2,147,483,647. So if you assign value which is not in this range then compiler would give error.
  1. // compile time error: Cannot implicitly convert type 'long' to 'int'.
    int i = 21474836470; 

Alias vs .Net Type

The above table of predefined data types are actually alias to their .NET type (CLR class) name. The following table lists alias for predefined data types and related .NET class name.
Alias.NET TypeType
byteSystem.Bytestruct
sbyteSystem.SBytestruct
intSystem.Int32struct
uintSystem.UInt32struct
shortSystem.Int16struct
ushortSystem.UInt16struct
longSystem.Int64struct
ulongSystem.UInt64struct
floatSystem.Singlestruct
doubleSystem.Doublestruct
charSystem.Charstruct
boolSystem.Booleanstruct
objectSystem.ObjectClass
stringSystem.StringClass
decimalSystem.Decimalstruct
DateTimeSystem.DateTimestr

Conversion

The values of certain data types are automatically converted to the different data types in C#. This is called implicit conversion.
The following is an implicit data types conversion table.
Implicit Conversion FromTo
sbyteshort, int, long, float, double, decimal
byteshort, ushort, int, uint, long, ulong, float, double, decimal
shortint, long, float, double, or decimal
ushortint, uint, long, ulong, float, double, or decimal
intlong, float, double, or decimal.
uintlong, ulong, float, double, or decimal
longfloat, double, or decimal
ulongfloat, double, or decimal
charushort, int, uint, long, ulong, float, double, or decimal
floatDouble
Conversions from int, uint, long, or ulong to float and from long or ulong to double may cause a loss of precision. No data type implicitly converted to the char type. 
Conversions from int, uint, long or ulong to float and from long or ulong to double ma cause a loss of precision no data type implicitly converted to the chat type.












Post a Comment

0 Comments