C# is a very meticulous language. Every piece of data it encounters must have a type.
The type of the data determines the set of values that it can have, and also which operations can be performed with it. Depending on the syntax, C# can sometimes infer the type of the data.
Remember our Hello, World program earlier? We needed to enclose the text
Hello, World! in double quotes (
"") for it to be a valid value. Enclosing the text in double quotes made that entire piece of data be of the string type.
C# has many built-in data types. Some types have a special keyword in C#. We'll learn where these keywords are used later.
Here is a subset of the basic types you will likely encounter:
The string type can contain text values. The text can have letters, numbers, and symbols.
The length of the string can range from a single character to a whole paragraph - it can even be empty. They need to be enclosed in double quotes.
"To you, 2000 years from now"
The integer type can contain integer (whole number) values. Integers can have a negative value and can be written as-is.
The boolean type has only two possible values, true or false. Booleans are used in conditional expressions (which we'll learn in another lesson.)
The character type can contain a single character. Characters can be letters, numbers, and symbols. They need to be enclosed in single quotes (
The floating-point numeric type can contain numbers with fractional values, represented in decimal format. Like integers, they can be written as-is.
double (short for double-precision floating-point)
Console.WriteLine() doesn't just take string values. It can also print out values of other types.
Try printing out each example above by calling
Console.WriteLine() and replacing the value in the round brackets.
// StringConsole.WriteLine("To you, 2000 years from now");// IntegerConsole.WriteLine(99);// BooleanConsole.WriteLine(true);// CharacterConsole.WriteLine('a');// Floating-Point NumericConsole.WriteLine(3.1416);
Run the program - you should see the values in the console output.