Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We'll learn about more integral types and parse more data from our CSV file.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've got more data in our CSV
file we need to add properties for
0:00
in our GameResult class.
0:03
Let's check out our CSV file.
0:05
Looks like the next four fields are Goals,
GoalAttempts,
0:07
ShotsOnGoal, and ShotsOffGoal.
0:11
They all seem to be
positive whole numbers.
0:16
Let's talk a little more about
the different integral types in C#.
0:21
You've seen bytes and integers.
0:25
All of the integral types we'll be
covering here have a signed and
0:27
an unsigned version.
0:30
Let's recap.
0:32
A byte is an 8-bit unsigned integer.
0:33
It can store values from 0 to 255.
0:36
The signed byte, SByte,
can hold values from -128 to 127.
0:39
It's very common and
0:45
usually recommended to just use the int
keyword when creating integers.
0:47
The int keyword represents
a signed 32-bit integer,
0:52
which, if you remember, can store
both negative and positive values.
0:55
It has a pretty big range from
around -2 billion to 2 billion.
1:00
We can demystify these seemingly arbitrary
minimums and maximums a little bit.
1:05
Our byte type is 8-bit,
it can store values from 0 to 255.
1:10
2 to the 8 power is 256, which is
the number of values a byte can store.
1:16
Our int type or Int32 is a 32-bit integer.
1:22
So, it can store a number of values
that is equal to 2 to the 32nd power,
1:26
which is a pretty big number.
1:31
I'm going to use
the interactive window here
1:33
like a calculator, Math.Pow(2,32).
1:38
Since the int keyword represents
a 32-bit signed integer,
1:46
it has both negative and positive values.
1:49
It can represent around 4
billion different values.
1:53
Luckily for us, if we really need to know
without looking it up, the int type has
1:56
a static property on it that tells
us the min value and the max value.
2:01
int.MinValue and int.MaxValue,
2:05
the unsigned version of
a 32-bit integer is a uint.
2:09
The u stands for unsigned.
2:16
It only holds positive values and
2:19
goes up to the 4 billion
number we saw earlier,
2:22
uint.MinValue, uint.MaxValue.
2:27
The least common of the integer types is
a short, it's a 16-bit unsigned integer.
2:31
So it can store 2 to the 16th number
of values, which is around 65,000.
2:37
short.Min and short.MaxValue,
2:42
and its unsigned counterpart, ushort,
2:47
ushort.MinValue, ushort.MaxValue.
2:53
You might see them, you might not.
2:59
If you consider using a short type,
I'd probably just go with an int.
3:01
But if you happen to need an integer that
can hold more than 4 billion possible
3:05
values, that's where the long
type comes in, long.MinValue,
3:09
long.MaxValue.
3:15
It can come in handy for
really big numbers.
3:19
You'll see it in the .Net
framework pretty often.
3:22
I can think of a property
that uses the long type.
3:25
You saw DateTime earlier,
it has a property on it called ticks.
3:28
DateTime.Now.Ticks, ticks
are 1/10,000 of a millisecond.
3:33
So, it has the potential
to be a really big number.
3:41
It also has an unsigned version,
the ulong,
3:45
ulong.MinValue, ulong.MaxValue.
3:51
I've linked to an MSTN article that
lists all the integral types and
3:56
their values for reference.
4:00
For most purposes,
4:02
using the int keyword to specify a 32-bit
signed integer is the way to go.
4:03
So let's do that in our GameResult class,
4:09
public int Goals { get; set; },
4:15
and public int GoalAttempts
4:21
{ get; set; }, and
4:26
public int ShotsOnGoal { get;
4:30
set; }, and the last one,
4:36
public int ShotsOffGoal { get; set; }.
4:41
So now we need to parse these
values from our CSV file.
4:51
We can use that with
the int.tryParse method.
4:55
We can even use the same variable for the
L parameter as we try to parse each one.
4:59
Let's see, int parseInt and
5:05
if (int.TryParse(values[3],
5:13
out parseInt)), then we'll
5:21
assign gameResult.Goals = parseInt.
5:27
And let's do a little copy paste magic.
5:37
We've got four properties,
so one, two, three more.
5:42
Then we'll change values 4, 5, and 6.
5:47
This is gonna be GoalAttempts and
5:55
ShotsOnGoal and
6:02
then ShotsOffGoal.
6:06
Let's take a look at our progress.
6:11
We still got our breakpoints,
so we'll debug with F5.
6:13
All right, things are coming along nicely.
6:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up