Hey guys welcome back.
So this time I'm going to open my project for using Visual Studios interface.
So I open Visual Studio and then to the left they're showing me all of the recent projects that I have
done so I'm just going to select this my project from the list.
I can also pin it because maybe you know I have a bunch of projects working on and I want this one to
be at the top always so I can just print it and then I can just go ahead and click it and open it and
then Visual Studio will launch with my projects.
So Visual Studio is putting us right back where we left off which is when we were doing that input output
program.
But this time we're going to be looking at some data types so I'm going to create a new project.
So that the solution go to
ad and then select new project
and then when the console comes up the dialog box comes up.
I just select console Dalton core and then click next and then this time the project name that I want
is variables and data types.
So variables and data types and then I click Create.
Now I did say that C Shabaab boasts a wide collection of data types.
There are simple data types and there are user defined data types so that the basic ones that you have
with all those the simple data types which are like the string and you know four numbers and so on now
we're going to go through some of them.
That being said there are a lot so I'm not going to go through every single one.
And along the way we may end up using one or a few that we didn't cover in this video.
So I'll go through the basic ones that you definitely need to know.
But as we go along you will see others that contextually you may need.
I also want to point out that C sharp is what you call a strongly typed language.
So that means that if you are going to declare a variable that should hold text then it's going to treat
it like text.
So even if I put one as in the numeral one instead of a text variable it won't treat it like a number
to do math with it.
So you have to choose your data that was very carefully based on what operation you want to carry out.
So let's start off.
So we did see string.
So let me say variable declarations and type someone to use this section just define some variables
and we can look at what they are.
So one string.
So we went through a string pretty string and I'm going to say name then maybe you will want to collect
the age also.
No.
Are we going to store somebodies age as string.
Or are we going to store their age as maybe a number.
Because if I tell you I'm 10 years older than you.
Then you want to do some math to see okay as a 10 year old.
This is mathematically whole you're going to be X Y Z or something like that.
You know you may want to do a mathematical operation.
You cannot use a string variable for math so you want to use what you call an INT.
So into short for integer and that one an integer is basically a number that doesn't have any decimal
places.
So if you put a decimal number inside edge like you say they are twenty two point five years old because
an int is going to ignore the point five.
You do have other variables that call it that so I could probably make one that's a salary so I could
say double salary.
So double will take a decimal point.
All right.
You also have another data type called float that does this small points but then based on the number
of decimal places you know that that's where there really difference the number of decimal places on
the precision that is a load.
In simpler terms you know you may have like a value like Pi which we all know as a decimal is three
point one for something something something something something double.
I'm floored me.
End of story and different values because one will be more precise than the other.
You also have what you call a char so a char is short for a character and a char variable is usually
used for a single is not usually used.
It is used.
Sorry for a single character so case study or a case for that would be like gender.
Right.
So charge under you're either m or f.
Another very popular basic data type that we would see is boolean.
So we have bool and then I went to use this one to see it working.
So bool means yes or no.
It's either true or false.
It is either one or zero.
There is no other value that can go in said a bool but true or false.
So let's recap real quickly.
So we have string name so string takes takes the takes.
Anything that is it can take any alphanumeric and character combination is just going to receive something
and then print it back.
You can't do any math on the numbers you put in there.
You'll have to do conversions and it while it's relatively easy to do conversions I just recommend that
you use the right variable when you need it for a certain thing.
So if you have a string it's clearly not to do any math.
If we have an integer it's because we want a number and we're not expecting decimal places.
If we have a double that means we can get a number and we might have decimal places.
We have char because we're expecting one character.
This one character can be of any first equation meaning can be a letter a number a special character
but it's only going to be 1 so you can put your name inside of a char.
All right.
So for this example we're going to accept M or F for gender and then we have the boolean right which
can only be true or false.
Now we just wrapped up a session of both prompting users for input so what I'm want to do is from them
for their name their age their salary their gender and if they're working or not.
So I'm just going to do about two of them and then I'm going to allow you to try and do the rest.
So I want to say console dot and then remember we have to print to the screen.
Hey I'm asking you for this data right here and someone to use right.
And then you will see the difference in the problems when I use right instead of right line.
But then I'm prompting this time for a name someone to say enter your name colon.
And then after that I'm going to say a name should get b get the value from the consul's.
The name is no equal to console dot read line and open and close burning the seats.
That's the name prompt.
Right.
No.
To expedite this process I can just copy if I want I can just copy those two lines go down and paste
and then I just change all the text so I asked for a name.
This time I want age and then the pattern is the same.
All I have to say is age is equal to console dot redesign.
No notice I'm getting an arrow and I'm asking for that age all right.
And then that's where r conversions come into play.
So I did say that this is variable data type and conversions right.
So in this situation it is seeing that there is an error cannot implicitly convert type String to int
No.
This didn't receive that arrow because console dot read line is getting some keyboard input from the
console which it is transporting to the variable in the form of a string.
So in this situation I did say that it strongly type sitcom com puts a word inside of a variable that
is expecting a number.
So this number variable is expecting a number to be input but the mechanism we're using to get the data
is transporting a string.
So that's a problem right.
So no we have to run what we call a conversion where we accept the string converted to it and all of
that before it gets into the variable.
So what I'm going to do is erase this and I'm going to rewrite this line step by step.
So C sharp gives us tools to convert and literally they give us a class called convert.
So we can write the word convert and then see a dot and then we have a number of conversions available
to us.
We can convert it thirty in to double to date time all sorts of things.
So I want to int thirty two and then I open.
So this is a function right.
And we do in all functions is because they have open and close parentheses.
Then inside of this function because notice is still as I red line it saying that it expects some value.
All right.
So no I need to tell it what exactly I want to convert and what I want to convert is the input coming
from the console.
So I'm going to call the console that read line when it gets there it's going to prompt the user is
going to pause get the input when the user presses enter.
So remember that this pauses for input until it sees enter.
Once the person presses enter it's then going to convert it to int or 32 bit signed into Joe.
All right so just does a fine so they'll see and puts it in an integer and you did see it too.
You did see two thirty two in thirty two and two into sixteen.
Anyway I've seen on the numbers.
The fact is the larger the number is the more bits required.
Right.
So two thirty two is pretty much standard.
We can leave it with thirty two and then once it is converted then it is assigned to our age variable.
I notice all is well in the universe.
There are no more errors so I'm going to do the rest and I'm not going to do any conversions just yet.
I'm going to pause here and I'm words I love you to try and write the problems for each of the other
three variables and then we can look at the potential arrows together.
All right.
The rest of my problems and I change that takes accordingly.
Enter celery enter your gender and then for the one with working I kind of change the verbiage because
once again you want to tell the user what kind of data you're expecting from them so you don't want
to leave it up to them to infer what they should be putting in.
So I said Are you working.
True or false sense yes but true wrongly so true or false.
So we're expecting them to enter the word true or the word false but more importantly notice how many
errors we have and the same reason that we had the error for it.
It is the same reason we're having these errors because consulate read line is returning a string but
the string cannot be put into a double.
Just like that kind of input into a char does that.
And it can't be put into a boolean just like that.
And as I said Char I noticed that I did not explicitly tell them that I'm expecting a more f so if the
person enters the word male or the word female then that will cause a problem because this is only expecting
one character the one letter one one not a word.
Right.
So I'm going to be a bit more explicit with this prompt and I'm going to say m or f all right.
So with all of that done no let's fix these conversion issues so it's the same thing we just have to
see convert dots to double and there we have to double and then what are we converting work on inverting
console dot redesign.
So I opened the parentheses here.
I need to make sure I close it.
So this is a common error or open it's maybe we'll get up and go and get coffee and come back and then
we look at it's one time I always say oh it's cool is because there's a close one but we're not paying
attention right so it's always good to open and close in the seam sweep and then you can get distracted
afterwards.
That was spare a lot of heartache.
So there we go.
Bubble is sorry a seller is expecting a double.
So we're getting the input from the console which is going us takes converting it to double.
So whether or not it does this some other places it will treat it like it does and then it will put
it in a W for char we have convert not to Cha.
So you have a convert for almost every single data type that is in C sharp right.
So you have quite a few data types.
I have quite a few ways to convert them and I hope by now you realize that.
Well I did see key symmetries when we were creating our projects.
Well in the language itself keys does matter because if I write convert as a with a capital C that will
never work because it doesn't know what converts with a common C sorry if I write convert with a common
c it doesn't know what that is.
So it only detects when I adhere to the strict rules and use my uppercase C for convert not console
convert and then I say dot and then I have to boolean as an option and then I open parentheses and then
I make sure to close my for and this is.
And there we have our functions or our code our problems taking data and storing them into the different
variables.
No once I've done all of that I want to print this person's profile back to the screen so I'm going
to do a little difference here with my printing to screen and we're going to call this concatenation.
So this is the process of joining strings together.
So earlier I would have had one console write that printed the static string and then another one to
print the variable.
This time I just want to write one console out right and I'm printing both the static String seeing
your name is called on and then I'm putting the variable inside of that.
So it gets printed automatically right.
So there are many ways to do it.
I'm to show you.
Maybe the easiest way right now and as we go along I'll probably do it the other way so that we can
get up and running.
Actually I think we have enough data to print to look at all of the concatenation techniques so I'm
just going to go through all of them.
So I'm just going to write the first one.
Sorry to print the name someone to C console.
That's right open parentheses and I'll just put my semicolon there seemed fine.
Everybody codes differently.
I just like doing it at the same time.
And then I went to say your name is colon.
And then I can use a plus sign.
So we all know that the plus sign is used in math right.
Well when we're talking about strings and printing sentences or printing to the screen we can use the
plus sign to say I want to add this block of text to whatever other block of the second of as many blocks
of text as I want and plus signs in between.
All right I can do that but in this situation what I want to add to this static block of text is the
value that is inside names so I'm just going to say your name is static text plus name.
So this is concatenation.
So this will print one line seeing your name is.
And whatever value was collected from the user at the time of input.
All right.
So I can just duplicate this or what.
No.
Let me write another one.
So console not write and then this time I want to print your age so your age is cool on and then the
next way I can concatenate is to use like placeholders and kind of have an array of variables to match
these place holders.
So I mean let me just demonstrate here.
So I'm going to open curly brace use a placeholder and that's 0 and then is fairly obvious and then
what is going to say is I know I'm need to print your name is and then I'm going to print this static
text but then this as I said is acting as a placeholder so I need to define something that should be
put into this space holder and what I do is after the block of text I press comma.
And then I can insert a variable so the variable that I want to be inserted into the place holder is
age all right.
And then I'm going to print the salary to the screen so same treatments console.
But right and then this time I'm going to do what we'll call interpolation.
All right so this one I'm going to start off with a dollar sign at the start of the string so it's a
dollar sign.
Open and close quotation marks and then I want to see a dollar sign your salary is on and then interpolation
allows me to just use open those braces and write the variable name right there inside of the static
string so it is summary so I have one static string I just have to make sure I have the dollar sign
it goes without the dollar sign then it will look more like the place or the code.
All right.
And that would not be desirable.
So with the dollar sign instead of using a placeholder I'm just opening the curly brace and putting
the variable itself inside of my contiguous string contiguous means continuous right.
So all of this string is just one block and we have our variable being rendered inside of that.
So the reason I'm showing all of these I can guarantee you this one will work.
And this one will definitely work the based on the version of dot net framework that you're using and
that influences the version of C sharp that you are using.
You may or may not have access to the interpolation.
Right.
So as I said there are many ways to do it and it's good to understand and appreciate whole.
But I can guarantee you if these two don't work this one definitely will.
And if this one works this one definitely will.
So you can pick you're about to pick whichever one you like the most and even more specifically whichever
one actually works in your context some way to proceed with the simple one that I know works universally.
And then I'm just going to make sure that I print the other two variables so this one says your gender
is and then I'm printing gender and then the next one is are you working.
Are you employed you are employed and we expect to see true or false.
So then working all right now all of this is going to print what does one big block of text it's going
to look disgusting on the screen which I'll show you why.
So let's test it out.
So my start project needs to be set of variables and data types so I write variables and data types
set a start up project and then go ahead and start.
All right so let's take our program for a test drive someone to say a test user is my name.
I'm 22 years old.
My salary is two thousand five hundred point thirty.
Press enter my gender is m enter and my walk working.
True I'm working.
And then when I press enter you see that okay we just get one long line everything in one line those
spaces.
It's very difficult to read and that's because we wrote with console the right and not console right
line.
Right.
So that's why we have to be very careful when we're using console rights and console that right line.
So I'm just going to change this OS to console the right line and I can just copy and paste and do that
mass replacement and then let's just run again and see how the old looks.
So we start off with our name and then once the city's female user her age is 23 her salary is five
thousand point four.
It's our five thousand five hundred.
Gender is F and then but then she's going to be unemployed.
So working is falls.
And then when we do that no we see the brindle looking much better.
We have a line per bit of information or just we have our static text and then it is printing buck the
value.
All right.
So the conversion was almost it really old and needed to be one way because noticed that a console is
printing back the values we didn't have to convert from Char to string and we didn't have to do that
to concatenate onto the string right.
So it's strict in some aspects and it's flexible in some.
So once again when we're collecting this string from the console we have to convert it into whatever
e to type the variable that is receiving the data is all right.
So to do int conversion to stored int w to store the W et cetera et cetera but then we were printing
back to screen we didn't have to convert from that data type string to get it was attached to our string
right.
So everything when it's printing all day it will just contextually say OK yes I know this is a boolean
but let me just print it.