1.2: Data Types Table
In this module we'll study types and look at some types challenges.
Types Within C#
Take some time to read through the table below. You can use it as a reference in the future. If it helps you learn, feel free to type the code in each of the rows:
Type
Description
int i = 0;
An int is a whole number with a size of 32 bits and range from -2,147,483,648 to 2,147,483,647..
short sh = 32767;
A short is a whole number with size 16 bits and range from -32,768 to 32,767. In the expression we assign sh to 32767. What happens if it is 32768.
byte by = 255;
A whole number with size of 8 bits and range from 0 to 255. In the expression we assign by to 255.
string s = "hey!";
A collection of characters with a size of up to 2 gigs or 2^32 bytes, or Int32.MaxValue. In the expression we assign s to hey!
char c = 'i';
A single character with a size of 16 bits. A string is just a linked list of chars. In the expression we assign c to the letter i.
var s2 = @"a \tree";
The @ symbol is used when about to call a reference tree as to specify a directory file on your machine such as \DotNetPreWork\HelloWorld.
bool b = false;
A boolean is a true or false statement. In the expression we assign b to false.
long l = 7;
Long is an integer data type that can just hold more than a normal int type. Exactly 2 times more. The size is 64 bit and the range is -9223372036854775808 to 9223372036854775808. Another way to assign this type is Int64.
decimal p = 99.99999999999999999999999999;
A numeral that can go to a 128-bit precision decimal with a range of 28-29 decimal places. A very costly data type that takes a lot of energy for the program to run. A decimal data type is often used to represent money or other data that needs to be exact.
double d = 7.80000000000000;
A numeral that has a 15-16 digit precision with a 64-bit size.
float f = 10.8f;
Floating point integer, must put f explicit conversion afterwards. Float is similar to a double only it is stored as 32 bits within the memory.
decimal dd = 7.80m;
Without the suffix m, the number is treated as a double and generates a compiler error.
int? ni = null;
When there is a ? afterwards it means the value can be null. Null means it can either have a value there or it can be empty. Null can be used when you are not sure if there will be a value or not such as with web services or database results. Use .HasValue to see if the current Nullable object has a valid value of its underlying type.
Challenges
Use what we've learned so far to complete the challenges:
File Location
Right click on the solution you made in the first module, go to Add > New Project
Select Console App (.NET Framework)
Name it
0.02_Types_Challenges
Write your code for this module within
Program.cs
of that project
Important Note: Now that you have more than one project within your solution, you will need to specify which project will run when you click start or press CTRL F5
. 1. In the Solution Explorer right click on the project you are working on 2. Select Set as Startup Project
Bronze
Create a list of value types: int, string, bool, float, double, decimal
Do two examples for each type
One is a declaration only
The other is a declaration with initializer
Print some of the values in the console
Silver
Use two different types to concatenate a single string, any string will do
Hint: The
+
operator can be used with strings
Gold
Declare the year you were born as a string using a birth year variable with string interpolation.
Last updated