138 Video Views·Apr 22, 2025
C# (C-Sharp) is an extremely versatile programming language and is used as the driving code in a large number of applications including Unity for game design!
This tutorial takes you through everything you need to know about String and Numerical data types, including float, double and decimal data types. We then walk through a number of the most common built in tools for manipulating and processing data using methods. Understanding these data types is absolutely crucial for getting started with C# and this tutorial can get you there!
This is the second C# tutorial on this channel and I will start mixing these in with the other software tutorials we do going forward!
If you did enjoy this content or found it useful be sure to leave a like and subscribe to channel for more great content like this, and be sure to check out the channels for hundreds of useful tutorials!
Thanks for watching and good luck with your code!
what's going on you guys welcome back to
the channel and today's video is going
to be a discussion of the string and
numerical data types within c sharp
[Music]
so today's video is going to be the
second video in our introduction to
c-sharp tutorial series and if you are a
complete beginner to c-sharp like you
still need help downloading microsoft
visual studio or any ide knowing what to
install and how to open your first
console.net app then i recommend you
check out the first video on the channel
otherwise we're going to get into some
pretty beginner
c-sharp concepts in this video hope you
enjoy it
so to start with i always just inside my
function i'll put this right line read
line because those are pretty typical
input and output instructions
but to get started i just want to create
a string variable so we talked a little
bit about variables in the first video
but if this is your first time seeing
this that's fine too so to define a new
variable and define it as a string you
start by typing the word string
and then you type the name of the
variable that you want it to have i'm
going to call my string my string
and then you set an equal sign and then
you use the two um the two
quotation marks to identify that it's
going to be a string and this is an
important difference between c-sharp and
like python because in python single
quotes and double quotes it doesn't make
a difference it does in c-sharp if you
use single quotations it's a character
and you can only put one
letter or number in there but if it is a
two quotations then it's a full string
and you can have as many
text letters as you need and so inside
the quotations you can put your text so
i'll just say pete rocks
because that's good text and so now if
i'm going to go ahead and use my right
line
i'm just going to display my string and
then the read line is only here to slow
the program down so that we can actually
display it but if i go ahead and run
this
we get pete rocks on the console window
okay so that's great and if i were to
hit enter then that's going to finish
the read line and then our code is going
to close or you can hit the x whatever
but
so this is just me putting in a variable
because it's easier you could also pete
does not rock you can also type a string
directly in the right line output and so
if i were to run this you'll see we're
not using my string anywhere anymore
we're just using that text directly
that's valid too but a lot of the
concepts i want to show you in this
video
are going to require um a variable your
string being stored somewhere so we're
going to go ahead and put it in my
string and i think the next thing i want
to talk about are just a couple special
characters so um if you want a new line
so say i wanted rocks to be on a second
line
it's backslash n and you should see if
you're using visual studio and if you
still have it on default text editor
settings which i would recommend for
beginners when you do backslash n it
turns yellow because it's a special
character um but so now if i run this we
get pete and then we get rocks on the
next line
and that's because backslash n is our
new line special character
a couple other things if you want a
quotation mark so if i wanted pete to be
in quotation marks well the problem is
it thinks that a quotation is either the
start or end of a string so what you do
is backslash again and then quotation
marks and then you would do the same
thing at the end
so backslash quotation marks and that
should turn yellow as well it's just a
special identifier saying take this
thing that normally would not just be a
string character and make it a string
character and so the last one i want to
touch on since we're here is if you want
a backslash you do two backslashes
straight just like that and that's going
to write just one backslash onto the
screen okay so those are all kind of
special cases you may want quotations
you may need a backslash and you may
need a new line at some point so those
are just good to know
um
okay so the second thing i want to talk
about is concatenation so if i were to
make a second string i'll call it second
string
and in this one i will say
but not
that much
now i have these two strings and if i
want to display them both i can do it a
couple different ways and the easiest
one is just where i'm displaying the
first string use this plus and then type
in second string and this is called
string concatenation
so
anytime you combine multiple strings
it's called concatenating your strings
that's just what's called you can call
it string combining if you want and what
you'll see is they actually smush right
together because we don't have a space
built into that so what you could do is
you could put a space at the end of one
string or you could put a space at the
start of the second string either way it
kind of looks weird just on its own so
what else you could do is you could do a
plus and then two quotations and then
just a space and this is you manually
inserting a space character into your
string concatenation but now you can see
it looks good
okay so that's a little bit about string
concatenation that's a little bit about
um strings in general but now let's
start looking at things that you can use
with strings so um
everything we're about to use is called
a method so it's like a built-in string
manipulation or an analysis tool that
c-sharp and visual studio have built in
so the first method i'm going to talk
about is length so if i type in my
string which is pete rocks and i do dot
and then length and you'll actually see
right away when you put in a string and
then a period you see if you're using an
ide like visual studio a good modern one
it shows you all of the built-in methods
that you can use to manipulate strings
so we're just gonna do a few simple ones
today a few common ones
but you can always scroll through there
and look if you're looking for a more
advanced function it could good chance
that it shows up in there so the first
one we're going to show is just my
string dot length and that's all you
have to do right there and what you're
going to see is it's going to tell us
how many characters are in this string
so this is five
and that'll it should show us 10 i think
there we go so it shows us 10 and that's
because space counts as a character
but one thing i want to show you if you
have those special characters so like
backslash n for a new line this will
still work it's just going to tell you
11 and you may notice we added a
backslash and an n and it only added one
character that's because backslash n is
considered one character so is backslash
quote backslash backslash they're all
special characters but in terms of
length it's just viewing that as one
character so that's the dot length a lot
of the times you can use this in things
like a password checker to make sure you
meet the minimum length requirements
things like that
okay so the next thing i will talk about
uh the next method is if you want to
make sure you have a specific case so
let's say instead of pete rocks this was
the master tech okay and i'll get rid of
this second string for now because we're
not using it so if you had something
where you wanted to pass um
all of the the the string in but you
want it to be all uppercase there's a
built-in method for that it's string dot
2 upper like that and then you need to
add parentheses right there and so if i
go ahead and run this you'll see we get
lemaster tech all caps that's pretty
good and you probably could guess
there's also two lower just like that
and that's going to give us the master
tech a lowercase
great um so that's two upper two lower
and then the next thing is you can check
if a string contains a subset of that
string so i'm gonna go ahead and just
say let's say it was checking just for
the master
and this is going to return just a true
false so there you see we get true
because it's in the master tech
but a couple things about this
if it is case sensitive so if you were
to check le master spelled that way
it's going to say false even though
those letters are in there so what you
could do is you could combine something
like two upper and the contains
statement to uh to kind of combine these
operations so you could say contains and
then two upper
of my string and then all caps lemaster
if you don't care about case but just
know that this is case sensitive and
then it's also if you were to say well
the master's in there but le masters is
not then it'll still tell you false it
needs to see everything that you put
inside of this
is in there
okay so that's useful now uh the next
thing i want to talk about is the index
of individual characters so if you have
my string and you want to look up
specific character
um you use these square brackets and
then you put in the character that you
want to see
so let's say i'm trying to get a hold of
this capital m the counting actually
starts from zero not one so l even
though it's the first letter
its index location is zero if you're
familiar with software or programming
concepts
the the first bit the first location of
anything is typically referred to as a
zero not a one so actually counting up
zero one two would give us the capital m
so let's go ahead and run that
and you can see we get a capital m on
the screen
okay so that's indexing you can you know
you can index anything i'll index five
here and we'll get lowercase t so that's
um it can just be useful sometimes to
look up the first character of a
of a string or anything like that when
you don't care about actually the whole
string
um all right so another thing since
we're talking about the indexes is you
can use this concept index of and then
you can look up where an actual sub
string exists so we let's say we want to
know
when the word text starts okay
so it's going to tell you the index of
wherever it shows up inside of your
thing so
tech starts at the ninth character which
if we count zero one two three four five
six seven eight is the space nine is the
capital t
it gives us a 9. that's great something
to note if you have multiple of a letter
though so like lowercase e there's three
of them it's always going to give you
the first index so once it finds your
letter it's gonna just give you that
value and then leave so that's the index
of it's uh pretty useful and so let's go
ahead and talk about the
substring now so this is another very
common method
and what you do here is you use that
index value
and if you only put one value in what
it's going to do is it's going to go to
that index so 0 1 2 3 4. it's going to
go to s
and that's just going to give you the
remainder of the string starting from
there so you can see we get ster space
tech
but there's a second thing you can do
with the sub string which is you can
also give it a length so let's say we
want to be six characters long
if we go in now we just get s t e r
space capital t so this is how you grab
a portion of your string um without
modifying the whole string and that's
the dot substring method okay so these
are a bunch of really useful string
concepts that it's just great to know
but we're going to go ahead and move on
and talk about number concepts now
because it's very similar so
as we talked about in the previous video
but if you don't remember that's okay so
if you use int
that's a whole number integer value so
that has to be something like 1 2 3 4 5
6 12. doesn't matter has to be a whole
number it can be negative it can be zero
but it can't have decimal places
if you do want to use decimal places
there is float decimal and double i
recommend just sticking with double for
all common especially beginner concepts
really accurate numbers a double is
going to be fine for you
and so for a double you can say we'll
say my
number because we're going to get rid of
integer for a double you can have
decimals you can say 32.5
things like that
and actually so i'm going to go ahead
and get those console.writeline and
readlines back
console.writeline
console.readline
just because most of what we're talking
about gets a lot better if we can
display it on the screen so just like
before you could just um you can just
put a number in your right line and show
that on the screen and if i run this it
works fine it shows me 35.8
um but it's typically if you're using
the number somewhere in your function in
your program you're going to want it in
a variable because that way you can
actually store it somewhere
um
okay so uh we're gonna talk about a lot
of the
um methods underneath numbers now
because they're not it's not exactly the
same there are a few things that you can
do after if you press dot but mostly
it's like comparing and um to string and
checking for equals and those are
actually
not as important when you're dealing
with numbers as mathematical operations
are
so what i'd like to do is i'd like to do
a second here console.writeline
because we're going to come back to my
number and we're going to use it
a few times but i'm going to do a
console.writeline and type in capital
math math so that is like the math
module inside of c sharp and visual
studio and if you use a period after
that you're going to see tons of common
mathematical functions uh you've got
trigonometric things like cosine sine
and tangent
logarithmic functions exponential
functions we're going to talk about some
of the most common ones real quick so
one of the ones that pops up all the
time is abs and that's just absolute
value so if you're familiar with an
absolute value
it takes whatever your value is and it
gives you the positive return of that so
if i put minus 17 in here you can see we
just get positive 17. it also works on
mathematical operations so if we had
like minus 17
times 2 so that's negative 34
it's going to do the math for us 34 and
give us the absolute value and i should
have mentioned that here as well so like
my number plus 12
it can do this math just built in
intrinsically and you're gonna see even
though our number was 32.54
it adds 12 to it and displays 44.54
okay so you can do
plus
minus
multiplication is the star and then
division is the forward slash
you can do any of those right in this
statement or you can do something like
um
double
and then my
math number all right no no call it
whatever you want um but you could you
could intermediate you could save the
value of the operation that you're doing
into an intermediate variable so if you
wind my number divided by 11
you could store it in my math number and
then you could display my math number
okay so that's gonna be like just shy of
three
five eight um so this is another way of
doing it you could just do this math if
it's not too complex write in your right
line statement or write in your output
wherever
or you can store it in an intermediate
variable this is probably a little bit
cleaner in general having a variable to
store your math is is a good place to do
it
okay so that was just a little tangent
let's go ahead and
just change this back to my number and
i'm going to keep showing you some
methods inside the math module
so another super common one is pow which
is basically raise a number to a power
so let's say something that we can
verify we want to raise two to the
fourth power okay
all you do inside of the pow function is
you say what number it is that you
putting an exponential value on and then
what power you're raising it to so 2 to
the fourth
is 16. 2 times 2 is 4 times 2 is 8 times
2 is 16 okay that was quick math
um but so this also works with decimal
values um so 2.4 to the fourth would
still work just fine uh it also works on
believe negative exponential values so
yeah 2.4 to the negative fourth is .03
who knew
um
so yeah this works with negative x
exponentials it works with decimal
values uh the pow function is super
useful and pops up all the time
next sqrt is square root
so that's just giving you a square root
of a number it's the kind of the
opposite of the pow
and so there you can see square root of
36 is 6. 6
sqrt we'll do
min max and round next and then we'll
actually get into some fun like code so
min is a useful one for returning the
lesser of two values and this can be
decimals or integers or i believe a mix
so yeah you'll see if i put in min
between 12 and 89.5 it's going to tell
me like the minimum is 12. but if i take
this exact same line and i change that
min to max it should tell us well 89.5
is your max value so there you go
um min and max there's a ton of times
inside of python
c sharp inside of visual studio and c
sharp where you're actually going to be
comparing multiple values selecting the
larger or the smaller of the two to
continue with so that's a super useful
concept and then the last
method i want to talk about inside of
the math module is just round and you're
probably familiar with like standard
rounding rules for
your um
for your function but let's say you had
17.9845
um so obviously that's pretty close to
18 you would expect it to round and
there you go to 18. if you uh put it
right at 17.5 it is gonna round down um
oh 17.5 round it up okay make me a liar
code
um
let's see 17.49 rounds down okay
interesting because i swear i had
17.5 earlier rounded down that's cool if
you have 17.5 it's gonna round up every
time
uh okay so
the round function super useful to know
um i believe if you do um a comma and
then you uh put an integer there you can
select how many decimal points you want
to round to so it doesn't have to round
to a whole number
so if you have 17.523
and you want keep two decimal points
it'll round to that second decimal point
if you had like you know something goofy
and you want to round to four decimal
points uh you can do that just by adding
a comma adding a second argument
so that's super useful um in actually
like
grabbing the amount of data that you
have a level of confidence in
and that is really those are the most
key things i wanted to talk about inside
of the math module so let me just say
two more things
about
like built-in functions that are going
to speed up your programming
so there's a way to increment your
number without having to do
the long form of this which would be my
number equals and then whatever my
number currently is plus one okay and
what you'll see because we've got 32.54
here
that's going to give us 33.54 which is
what we want but there's a faster way
and it would just be my number plus plus
and you're going to use this all the
time for things in like for loops and
integer uh while loops as like an
incrementer value
so you'll see there just by changing it
to plus plus it still gives us plus one
and that's a lot faster than plus you
know my number equals my number plus one
and then obviously the inverse of this
you can do minus minus and that's going
to
negate one so it'll go from 32.54
to 31.54
okay so uh super good to know all of
these things about numbers um one quick
thing if you do have a double and an
integer so i'll call this one whole
number and it's equal to three
and you multiply the two together or you
divide them or you add them or anything
if you have an integer and a decimal
number
and you combine them it's always going
to give you an output of the double it's
always going to give you the decimal the
more accurate value
so
97.62 when you multiply the two together
but a really important thing to know is
if you have two integers
and you do something that should give
you a decimal value so let's say my
number divided by whole number
right 13 divided by 3 should be like 4
and a third so 4.333
because we're using two integers it's
not going to give us an integer out a
decimal output it's going to give us an
integer output so the way around that is
just make one of them instead of an
integer
make one of them a double and that way
you just do 3.0 or you could do i think
i think you can do three
and it's still going to give you a
decimal answer so you don't have to type
this point o in you can if you want to
remind yourself it's not an integer
anymore
um but yeah so there's that super useful
tip the last thing i want to talk about
the modulus operator or modulo operator
one of those it's basically division but
then checking for the remainder so it's
basically a remainder operator so 13
divided by 3 is 4 with a remainder of 1.
if i run this
it gives us a 1. now 14 divided by 3
is still
is still
4 but now the remainder is 2. so if i
show that it gives us the remainder of
two there's a whole bunch of times in
programming you're going to want to make
sure that one number fits evenly into
another number so in that case you would
check that something was
essentially like division you'd check
that the modulus was equal to zero
that's how you could make sure something
divides evenly into something else
okay so this was a crash course in
strings and a crash course in numerical
data types and i hope you found it
useful and if you do have any questions
feel free to ask them in the comments
below if you did find it useful please
leave a like on the video subscribe to
the channel it helps me out a ton
and the next step in our tutorials we're
going to take everything that we've
learned and talked about
and turned into like a four function
calculator so stay tuned for that be on
the lookout for it and that's really
going to be the next step in learning c
sharp so thanks so much for watching and
good luck with your code thanks bye