C# int size, new, declare array, convert it to string, double, long, short, ushort, byte and sbyte

Lionsure 2020-08-25 Original by the website

1. C# int size

int is used to declare an integer type. It is an alias of System.Int32, with a value range of -2,147,483,648 to 2,147,483,647. It is a signed 32-bit integer, that is,it can represent positive and negative numbers.

The value range of data type is closely related to the occupied bytes. How many bytes does the int type occupy? It occupies 4 bytes, each with 8 bits, so 4 * 8 = 32 bits.

 

2. C# declare integer variable(new int C#)

int i = 0;

int n,count,p;//Default value 0

int n = 0,c = 1,p = 2;

 

int? i;//Add ?, the default value is null instead of 0

 

Declare int array C#:

int[] arr = { 1, 2, 3, 4, 5 };

int[] arr1 = new int[]{ 1, 2, 3, 4, 5 };

int[] arr2 = new int[5]{ 1, 2, 3, 4, 5 };

 

 

3. int is converted to other data types

A. int to string

int i = 100;

string si = i.ToString(i);

 

B. C# int to double

int i = 20;

double d = i;//Implicit conversion

double d = (double)i;//Forced conversion

 

C. C# int to long

long l = i;//Implicit conversion

long l1 = (long)i;//Forced conversion

 

D. C# int to short

short s = i;//Implicit conversion(error)

short s1 = (short)i;//Forced conversion

 

E. C# int to ushort

ushort us = i;//Implicit conversion(error)

ushort us1 = (ushort)i;//Forced conversion

 

F. C# int to byte

byte b = i;//Implicit conversion(error)

byte b1 = (byte)i;//Forced conversion

 

G. C# int to sbyte

sbyte sb = i;//Implicit conversion(error)

byte sb1 = (sbyte)i;//Forced conversion