2.0 State data types
2.0 State data types
Example
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 age = 19;
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);
The float and double data types can store fractional numbers. Note
that you should end the value with an "F" for floats and "D" for
doubles:
Float Example
float weight = 5.75F;
Console.WriteLine(myNum);
Double Example
double height = 19.99D;
Console.WriteLine(myNum);
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# Example:
myNum = 9;
myDoubleNum = 8.99;
myLetter = 'A';
myBoolean = false;
myText = "Hello World";