Monday, March 19, 2018

DataTypes And .Net Types (CTS)

As we already know that we make the variable to allocate the memory space in our program to store the values. But in programming languages, we always need to define the type of the variable like (int, float, decimal, short etc.). With the help of these datatypes, computer can understand what kind of container he allocate, how much the size will be of this container and how much the value we can store in this variable, what the operation we can manipulate on this container. These kind of things are actually defined by the data types.
In C#, we’ve many kind of built-in data types. Let’s discuss some basic data types one by one.
Alias
Type Name
.Net Type
byte
System.Byte
struct
sbyte
System.SByte
struct
int
System.Int32
struct
uint
System.UInt32
struct
short
System.Int16
struct
ushort
System.UInt16
struct
long
System.Int64
struct
ulong
System.UInt64
struct
float
System.Single
struct
double
System.Double
struct
char
System.Char
struct
bool
System.Boolean
struct
object
System.Object
Class
string
System.String
Class
decimal
System.Decimal
struct
dateTime
System.DateTime
struct
Look we use these data types which are actually the aliases of these Type Names and the .Net Types of these type names are also given above. Look int is actually an aliase of System.Int32 and Int32 is a struct under the hood. string is an alias of System.String which is a class.

Integer Data Type
Integer data type is commonly used to store the whole numbers. It is commonly used data type. We’ve different kind of datatypes depending upon the size and the values weather the integer is signed (positive or negative) or unsigned (positive only).

Signed Integrals are
Unsigned integrals are
In these documentation articles, examples and notes are important. So read them.

Let’s discuss the ranges of these data types.
Signed Integrals
long: (64 bits, 8 byte) -9223372036854775808 to 9223372036854775807
int:    (32 bits, 4 byte) -2147483648 to 2147483648
short: (16 bits, 2 byte) -32768 to 32768
sbyte: (8 bits, 1 byte) -128 to 127
Unsigned Integrals
ulong: (64 bits, 8 byte) 0 to 18446744073709551615
uint:    (32 bits, 4 byte) 0 to 4294967295
ushort: (16 bits, 2 byte) 0 to 65535
byte: (8 bits, 1 byte) 0 to 255

Fractional Data Types
Fractional Data Types are often used to store the real numbers. Here we also have different types of Fractional Data Types. Fractional data types are important in calculation scenarios which gives us accurate results like in banking applications.
Fractional Data Types, their sizes and ranges are
float: (32 bits, 4 byte) 1.5 × 10−45 to 3.4 × 1038, 7-digit precision
double: (64 bits, 8 byte) 5.0 × 10−324 to 1.7 × 10308, 15-digit precision
decimal: (128 bits, 16 byte)  –7.9 × 10−28 to 7.9 × 1028, with at least 28-digit precision

Suffixes Explaination
Let’s discuss fractional data types with their suffixes. Actually real numbers declared as double by default. And if we don’t specify the suffix then we’ll see error.

We’re seeing the red squiggle line because we’re trying to store the double value into float & decimal data type container and implicit conversion is not allowed here and we’re not watching any red line under double variable statement. But if we use the suffixes to describe the value types explicitly then we can resolve this problem.

String Data Type
Actually most of the beginners confuse between these two things string and String and even most of the expert developers don’t know the difference.
string is actually the alias of String (System.String), each and everything which we can do with the String we can do with string as well. As we can see both keywords String and string has same results. String is a reference type and we know reference type can be null. We can initialize the string as empty string ("") but we have another technique to make the string empty. Both are equivalent to each other but this second approach is better then first one.

 string name = String.Empty;  

Boolean Data Type
it is used to declare the Boolean variables to store the true and false values. And Boolean type can’t cast/convert into any other type but in C++.

Size Of Data Types
Yes we can find out the size of the data type in our program with the help of sizeof() function. This operator shows the size in byte.

 class Program  
 {  
   static void Main(string[] args)  
   {  
     Console.WriteLine(sizeof(int));  
     Console.ReadLine();  
   }  
 }  


.Net Type And CTS
As we already know that declaration of variables in .Net different programming languages like C#, VB, F# has different way. But these programming languages actually use the same base class library this is called Common Type System. So that 1 language program can easily understand the information of different programming language in .Net
Look we’ve 2 different projects in our Solution ‘HelloWorld’ for C# and VBProject for Visual Basic Console Application.

0 comments:

Post a Comment