C # Learning diary 04 --- Integer Data Type
In the previous article, we initially learned about some data types, which are not comprehensive. In the spirit of being conscientious and realistic, I will sort out the data types again.
Integer type of Value Type:
Integer type
Data Type |
Features |
Value Range |
Sbyte |
8-bit signed integer |
-128 ~ Between 127 |
Byte |
8-bit unsigned integer |
0 ~ Between 255 |
Short |
16-bit signed integer |
-32768 ~ Between 32767 |
Ushort |
16-bit unsigned integer |
0 ~ Between 65535 |
Int |
32-bit signed integer |
-2147483648 ~ Between 2147483647 |
Uint |
32-bit unsigned integer |
0 ~ Between 4294967295 |
Long |
64-bit signed integer |
-2 ^ 63 ~ Between 2 ^ 63-1 |
Ulong |
64-bit unsigned integer |
0 ~ Between 2 ^ 64-1 |
I remember that when I was in C language class, the teacher asked us to read this form and asked me to write it down. At that time, it was not good-looking and I thought it was useless to remember this, but when I run the following code, I know the teacher's painstaking efforts:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Example{ class Program { static void Main(string[] args) { short x = 32766; x++; Console.WriteLine(x); x++; Console.WriteLine(x); Console.ReadKey(); } }}
Expected results:
Is Mao negative ???? At this time, we can see the greatness of the value range. 3268 he is out of the Short value range;
There are many similar experiences. I remember a teacher once told us to write a program to input a person's phone number, and then output it. The deskmate said, "It's very easy. I just wrote it three times, five times, five times, and two times." The Code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; namespace Example {class Program {static void Main (string [] args) {int x; Console. writeLine (name :); String name = Console. readLine (); Console. writeLine (Phone number:); x = int. parse (Console. readLine (); // type conversion Console. writeLine (your name is: + name ++ your phone number is: {0}, x );}}}
The result cannot be run because the valid int value is exceeded by 11 digits of only 10 phone numbers. You can use the long value instead. Add the following to the input and output documents: Console. the plus sign + in the middle of WriteLine (your name is: + name) indicates connecting two strings, that is, to combine them into one. When the expression has a String type, for example
Int I = 9;
String Str = "HC666"
Console. WriteLine (Str + I );
Running output: HC6669
This is because when the expression contains the String type and the int type, the compiler automatically converts the int type to the String type (implicit conversion) and then connects it. Implicit conversion will be learned later.
In the spirit of endless learning, I have made a new discovery of the previous diary: I can use Chinese characters to define variable names without making any mistakes.
Using System; using System. collections. generic; using System. linq; using System. text; namespace Example {class Program {static void Main (string [] args) {String name = HC666; int age = 16; double height = 1.70; Console. writeLine (Name: {0}, age: {1}, height: {2}, name, age, height );}}}
Its real C # is compiled in multiple languages, not only Chinese, but also Japanese, Korean ...... Yes, but we recommend that you use English !! Practice English.