Classes, Object Instances and Constructor Methods in C # and Microsoft Visual Studio!

LeMaster Tech
182 followers
Apr 21, 2025
This video covers the basic info needed to create and use classes in C# (Csharp) programming inside the Microsoft Visual Studio Development Environment. Creating classes allows for complex user defined data types and producing more advanced programs with numerous variable types and methods called inside the main one.
Check out my personal channel for more fun science content!
https://www.youtube.com/@peterlemaster8310
This is a continuation of the C# tutorial series for beginners on our channel, so be sure to check out the rest and let us know if you want to see something else on the channel soon!
Thank you so much for watching and be sure to leave a like and subscribe for more great content coming soon!
what's going on guys welcome back to the
channel and today's video is going to be
all about classes objects and
constructors in c-sharp using visual
studio
[Music]
so this is a continuation in our c-sharp
tutorial series um and i just want to
say real quick thank you to everyone who
has suggested topics for videos asked
for help in the comments and left likes
on the videos and subscribe to the
channel it's helping me a ton just
getting this channel started
so to continue on in our c-sharp
tutorial series we've talked about basic
variable types and we've talked about
for loops and while loops and some basic
things you can do with code but one
thing we haven't talked about yet is
what do you do when you have a more
advanced variable type so we've talked
about just how to create a variable
that's a string like a name or or
something along those lines um
you know and and that's fine but
you get things sometimes in code that
need to have multiple attributes
assigned to them and essentially you
want to group those attributes together
and create a new data type that is
essentially a class in c sharp and the
way you make it is pretty easy and the
way you use it is pretty easy so we're
going to go ahead and take a look at
that today and i want to do it kind of
in a fun way so i want to do it by
acting like we're creating a class for
storing video game information so let's
say
you come over to your solution explorer
right click on your c sharp file
and then go down to add
and then go to new item and it should
give you this pop-up and
pretty much always right near the top is
going to be class but you can search in
here if it's not
and just go ahead and hit add class
and it's going to auto-generate pretty
similar to when you open up your initial
program and to get between the two you
just click these tabs at the top here
but okay so what we do inside of this
class is we define the attributes that
we want
in the outside world so if you think of
a few things you would want to store in
like a video game storage
class you'd want like the title of the
game and so if this were in the outside
world you would do this you would just
say okay string title and then equals
whatever because you're defining it but
here we're actually saying we want to be
like a generic variable that can be
called by the outside world so we need
to add this public before it
and then we just do a
semicolon right there and we don't
actually give it any initial value
because you don't set those variables
equal to any value when you define the
class unless you want to give them a
default value
they get set when you create a class in
the outside world so i'm going to copy
that and let's say a second thing we
want for the studio um or for the video
game like definition to store the
information about is what studio
produced it
and then let's use a double which if you
remember from our variable types is just
a precise number it's like a float or a
decimal value that can do more accurate
than integers and we'll give it a rating
like out of five stars and then we'll do
just one more so that i can show these
classes can handle data from various
different data types so that's
the really the power of using it is you
can use strings doubles ins you can put
functions inside of these and methods
that we'll talk about as well
but then we'll say okay what year was it
made
and for now i think we'll just leave it
at this and maybe so that you can see
what's happening when we go back to our
function
we'll come back to this but let's just
leave it here for now because we've
really just created it
but the thing we haven't done is we just
called it class one and actually
typically when you're making this i
recommend giving it a name that's going
to be a little bit easier to um
to know what's in there so when you
create it there's an option to rename it
but you also can rename it just within
your game and you can see there just
going down into file name
you can rename it and just make sure you
also call the class whatever you name
the file so we'll call ours game and
let's go back into the program and
actually make a couple of these okay so
the way you create a new instance
of the class that you just made and this
is what's considered an object is an
instance of a class
it's very similar to defining a new
variable you say game so that's the
class that we're calling and then just
your variable name and we'll call it
game one but you can call it whatever
you want it's a variable name and then
you say equals new
and then
game
and then
just open close brackets and we'll talk
about you can pass arguments in there
and we'll do that in just a second
but now what we can do is like okay
we've made the game so if i type in game
one dot
you get all of these variables that it
knows because it can see your class it
knows to expect year studio title rating
and so we can go in here and we can give
it that information we can say game one
title equals halo
we can say the studio
i'll try not to mess these up
the studio is real halo fans know the
studio is uh
bungee
if you type 343 in here you're too young
get out
and then we'll give it a rating so this
is like how good we thought the game was
out of 5 stars we'll say 4.5 it's a good
game and what year it released so halo 1
the original one
2001
but what's really cool is okay we've
just created all of this stuff and so if
i do something here if i do console.rt
and then i tell them we want to see
game1.title
okay
when i run this function
it should just print out halo onto the
screen because even though we didn't
create the class in this function
it's in our program and we referenced it
so i'll just go ahead and run it
and we'll see halo okay we got that
that's nice
it's not really too exciting right now
but where it becomes really powerful is
you can take what we just did and you
can change it all to game two and you
can put a second game in there and have
all the same driver code class code
behind it so let's say this is
super mario and that was made by
nintendo
and let's give that a rating of 4.0
and that came out in 1983 okay and so
now if i were to say game one title i
can also say game two title and
what you'll see
is we get both titles and that's great
you know that's the basics of a class
and how to create one and how to create
objects under a class but where this
becomes way more powerful is you get to
really start cleaning up this code
when you use what's called a constructor
and let's go ahead and do that next so
coming back into our class here
we're going to keep all of this just as
is
but we're going to create something
that's called a constructor and so to do
that we're going to say public and then
the name of the class again game and
then we're going to give it
open and close parentheses and then
we're going to tell it what sort of
arguments to expect so what i want is i
want from this line i want to be able to
just give it right here in these
parentheses the title the studio the
rating and the year and not have to have
these four lines of code in here
and so
we come in here and we say okay we're
going to pass in the title but we don't
just say string title like we would in
the outside world because title already
exists in this class so what you need to
do is you need to give it a different
name so i'll say arg title short for
argument title
you can call it whatever you want the
thing is you can't use exactly the same
name that you already used in defining
your class it'll make a little more
sense as soon as we get to the next part
but then let's say the second thing is
string we need the studio
next will be double
and we need the
r grading and then the last one is int
and we'll want the r gear okay
and now i'll be able to show you a
little bit better why we had to do it
that way
so if we come down here
we need to well we need the
container for this code
we need to
take those argument variables and assign
them to the internal variables of the
class so we've already defined title so
what we do is we say title is equal to
arg title and then we do that for every
one of them we say studio is equal to
arg studio and this may seem
like kind of a
extra step of tying stuff together but
it's really not because what we did when
we said um
title studio rating and year
is we said what their internal names are
going to be but then these are the
parameters that are actually being
passed in so i mean these could be
totally meaningless as long as you tie
them to the right thing and you know
what you're
tying them to when you define them it's
fine but it's just a better idea to give
them a name that's going to make it more
clear what you're doing
okay so you want it to look like this
you want all of your your
variables you're passing in to be mapped
to
um
to be mapped to internal variables and
to kind of show that this runs every
time we call
a object i'm going to do one thing as
well i'm going to say console.writeline
and i'm going to print this storing data
for
and then i'm going to also say the title
of the game and then just because it
looks a little better i'm going to add a
new line character
so basically every time we make a
new game it'll print out on the console
storing data for whatever game we're
making and then print an empty line just
to separate out these different things
that are happening okay so this is super
cool go ahead and save it and head back
into the main program what we can do now
is we can get rid of
these lines of code and i'll show you
how because we have this constructor and
it's giving us a red
line it should be giving you a red line
because now it's expecting parameters
passed in so now we give it a title
halo
and we give it a studio bungie
and we give it its rating and then we
give it the year it came out okay and we
can get rid of all four of these
and we do this same thing down here
super mario
nintendo
4.0
1983.
okay and now we get rid of these four
lines of code so you can see just doing
that we just went from 10 lines of code
to two lines of code
and so now i'll do the same thing i'll
show the game one title but what i want
to make clear here as well is actually
we're not going to do title because
we're going to show the title um
we'll
we'll do this this will be kind of cool
we'll say um game
game one
not with a capital g
game one dot title and then we'll add to
it
was
made
by
and then we'll
add
game one dot studio
and then
we'll add
in
and then last thing we'll add
gameone.year okay so i just want to show
that even though we never say here what
variable type
title is we don't say that this is going
to be title that's all being handled in
the class over here and then what we'll
do is we'll say game2 dot rating
is equal to 4.3 just because i also want
to show you can update those parameters
even after they're initially created so
that's what we're doing here and so then
we'll say here game two rating and we'll
actually say game two dot title
and we'll add
has
a rating of
and then we'll add the rating
so even though when we define super
mario brothers we gave it a rating of
4.0 during our function we decided to
update the rating to 4.3 okay so let's
go ahead and just run this because i
know we did a lot of stuff we haven't
run in a little while
so you'll see storing data for halo
storing data for super mario i guess i
put an extra caps in there
but these first two are happening in the
class in the ca in the uh class that we
defined separately but then these next
ones are happening in our main project
um so these top two are happening
because of the constructor function that
we made but then we're using that data
and manipulate manipulating it wow
so halo was made by bungie in 2001 super
mario's rating of 4.3 so maybe using
games is a bit of a silly example but i
think there's a really quick and really
useful introduction to classes objects
and constructors in c sharp
we're cut we've covered a lot of the
basic building blocks of various c-sharp
programs
of building more advanced c-sharp
programs and so what some of the
upcoming tutorials will be are actually
your first application you could build
with a graphical user interface or how
this might apply to
plugging into like a game engine like
unity how c-sharp can be leveraged there
so we will be diving into some of those
concepts if there are more basic
concepts that you would like to see and
see sharp before we get into too much of
the advanced stuff just let me know
about in the comments below um if
there's enough interest in a topic i'll
be sure to make a video on it if you
have found these useful please consider
subscribing to the channel leaving a
like on the video it helps me out a ton
i really appreciate all of the support
and as always good luck with your code
and thanks for watching thanks bye
