Tutorials 参考 练习 Menu
Paid Courses

C# Data Types


C#数据类型

As explained in the variables chapter, a variable in C# must be a specified data type:

Example

int myNum = 5;               // Integer (whole number)
double myDoubleNum = 5.99D;  // 浮动ing point number
char myLetter = 'D';         // Character
bool myBool = true;          // Boolean
string myText = "Hello";     // String

来试试吧 »

A data type specifies the size and type of variable values. It is important to use the correct data type for the corresponding variable; to avoid errors, to save time and memory, but it will also make your code more maintainable and readable. The most common data types are:

Data Type Size Description
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
bool 1 bit Stores true or false values
char 2 bytes Stores a single character/letter, surrounded by single quotes
string 2 bytes per character Stores a sequence of characters, surrounded by double quotes

Numbers

Number types are divided into two groups:

Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are int and long. Which type you should use, depends on the numeric value.

浮动ing point types represents numbers with a fractional part, containing one or more decimals. Valid types are float and double.

Even though there are many numeric types in C#, the most used for numbers are int (for whole numbers) and double (for floating point numbers). However, we will describe them all as you continue to read.


Integer Types

Int

The int data type can store whole numbers from -2147483648 to 2147483647. In general, and in our tutorial, the int data type is the preferred data type when we create variables with a numeric value.

Example

int myNum = 100000;
Console.WriteLine(myNum);

来试试吧 »

Long

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. This is used when int is not large enough to store the value. Note that you should end the value with an "L":

Example

long myNum = 15000000000L;
Console.WriteLine(myNum);

来试试吧 »


浮动ing Point Types

You should use a floating point type whenever you need a number with a decimal, such as 9.99 or 3.14515.

浮动

The float data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "F":

Example

float myNum = 5.75F;
Console.WriteLine(myNum);

来试试吧 »

Double

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you can end the value with a "D" (although not required):

Example

double myNum = 19.99D;
Console.WriteLine(myNum);

来试试吧 »

Use float or double?

The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore it is safer to use double for most calculations.

Scientific Numbers

A floating point number can also be a scientific number with an "e" to indicate the power of 10:

Example

float f1 = 35e3F;
double d1 = 12E4D;
Console.WriteLine(f1);
Console.WriteLine(d1);

来试试吧 »



Booleans

A boolean data type is declared with the bool keyword and can only take the values true or false:

Example

bool isCSharpFun = true;
bool isFishTasty = false;
Console.WriteLine(isCSharpFun);   // Outputs True
Console.WriteLine(isFishTasty);   // Outputs False

来试试吧 »

Boolean values are mostly used for conditional testing, which you will learn more about in a later chapter.


Characters

The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':

Example

char myGrade = 'B';
Console.WriteLine(myGrade);

来试试吧 »


Strings

The string data type is used to store a sequence of characters (text). String values must be surrounded by double quotes:

Example

string greeting = "Hello World";
Console.WriteLine(greeting);

来试试吧 »


C# 练习

Test Yourself With 练习

Exercise:

Add the correct data type for the following variables:

 myNum = 9;
 myDoubleNum = 8.99;
 myLetter = 'A';
 myBoolean = false;
 myText = "Hello World";

Start the Exercise





W3Schools is optimized for learning and training. 示例 might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2021 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.