3.0: Basic Types
Last updated
Last updated
In this module we will begin to learn about different data types in C#.
Types will be attached to any part of your application that has to do with your data and values. Every variable, constant, and expression that gives back a value has a type. Every method has a type for the input value. More on what methods and variables are soon.
Right click on the Solution you made in the previous module. Make sure you're not clicking on the
Go to Add > New Project and choose Console App (.NET Framework).
Name it 03_Types
.
You will write your code for this module within Program.cs
of that project.
As we build these console apps, let us give you an 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 when you press CTRL F5
. 1. In the Solution Explorer, right click on the project you are working on. 2. Select Set as Startup Project.
Here is a short table of types for C#:
Type
Name
Example
Description
Integer
int
20, -1500
A whole number with a size of 32 bits and range from -2,147,483,648 to 2,147,483,647
Float
float
1.5f
32 bit, non-whole number, up to 7 digits
Double
double
1.500000d
64 bit, up to 15-16 digits
Decimal
decimal
8.333333333333333333m
128 bit, up to 28-29 digits, more precise than double
or float
, but also more costly. Mainly used for financial situations
Boolean
bool
true, false
A true or false statement
String
string
"Hello World"
A collection of characters with a size of up to 2 gigs
These are some of the basic types C# uses and examples of when to use the different types.
Let's use the table above to add declare and initialize a few variables:
Use the above table to try answering the following question:
What is the best type for each of these items?
23
$3.33
15.25456
-3500
Hi!
Starting in the next module, we will provide a series of challenges for you to work on. Going forward, these will be presented as a series of increasingly difficult challenges. Here are how these challenges work: 1. Bronze: More practice with the current lesson(and some previous ones.) 2. Silver: A challenge that anticipates a concept in the not too distant future. 3. Gold: A harder challenge for a future concept. Feel free to research ahead.
Possible answers will also be provided in a separate module, but try to solve them on your own first. Additionally, there will likely be multiple ways to solve each challenge, so play around and find what works best for you.
We have made the following chart for your convenience. We hope this comes in handy for learning C# types.
Type
Description
int i = 0;
A whole number with a size of 32 bits and range from -2,147,483,648 to 2,147,483,647. In the expression, we assign i to the integer 0.
short sh = 32767;
A whole number with size 16 bits and range from -32,768 to 32,767. In the expression we assign sh to 32767.
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.