10. How to program in C# - CLASSES - Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello and welcome to video number 10 in the how to program in C sharp course so in today's video we're going to take a look at classes and until now we've pretty much only used one class in that bizarre main class and we've only used it to kind of group together our code but what we're going to be doing now is we are going to look at how classes can be used to kind of build our own sets of data that can then be distributed throughout our application and we can really take a look at how they can improve the way that we write code and allow us to write a whole new kinds of software so that was a very vague kind of explanation but classes are really really useful for a bunch of things and you you don't have to get very fine programming before you use them pretty much all the time so before we get started I just quickly want to mention that if you have any questions go to firmed up register con and also and I feel like I haven't mentioned this for quite a while we do indeed have a Facebook page so if you go to facebook.com slash Brad Keys you can check it out here and I do post quite a few things that doesn't appear on YouTube so if you want to follow along on the software I'm writing or updates like this by the way thank you so much for helping me reach 100,000 subscribers you are incredibly awesome and devoted followers and this could be done without you and yeah really just check it out we also I'm also on Twitter but Facebook is the place to be cool so what we're going to be doing now is we're going to open up summer in studio as always and you can see I've left a comment here that says classes so the first thing we're going to look at is creating a class so just like we have created a class here we're going to make another one just above that make sure it's still inside at the same namespace though and we're going to do that with typing class then the name of the class and kind of to give you a an understanding of what a class is I'm going to call this one animal animal is a pretty general term quite there exists many kinds of animals and animals have different properties and it's basically those kind of things that we try to explain using a class so inside a class we then give it some properties and that's of course done through variables so the first kind of property we could give it could be a name so we could make a string name and we can default this to may be spotty we could then also give it a an H into H equals and let's say that this one is six years old and then maybe just to get the float in there also we could do a happiness amount that ranges from zero to one and we could set that equal to zero point five by default so when we are writing things like this it's very important that we start to talk about what is called access modifiers and basically what access modifiers do is they describe from from where and how a piece of data or a variable can be accessed so now that we are juggling multiple classes it's very important that we know what this does you have noticed that sometimes when or until now when we've written a method we've made sure to make it a public static void where public and Static are modifiers and basically we have three types of modifiers so we are private or actually we have quite a few more but these are the ones that we are going to be using for now we have private public and stat private means that they the data can only be accessed from inside the same class public means that we can access the data from other class classes if we just reference the class and static means that it's not bound to an instance of the class but it's shared by all other instances so this is kind of confusing to you I totally get it but I will make sure to demonstrate what this means so right now we've just written written the data type then the name of the variable when we do this without including any modifier at all it defaults to private so this is the exact same as just writing string so if we put a private here in front of each data type and then tries and then try to access the animal class for this value it's not going to work instead what we'll have to do is put public here because right now we can only access it from within so if we have a method called void and we call this just test then from in here we'll be able to access name just fine you can see it pops up there and we can just maybe write out the name here and that's going to work just fine but if we want to access it from our main method then we're going to have to put public here instead so we're going to do that for all three variables then next up I just quickly want to talk about how you can instantiate a class so now we kind of have this idea of an animal that has these properties that can be changed so how do we create an animal well basically like any other variable with a datatype so down here we could go animal we're going to be creating an animal just like we go int and float or string we now go animal and we can call this maybe dark and set it equal to a new animal this is the syntax for doing that then we can write out dark dot name and you can see here that this creates a new animal and writes out the name spotty what we can also do is change things in this animal class so if we go back here we can access values in a class by just using the dot so we can say dark dot name should now be equal to I don't know test and then we can write out the dark that name again and you can see it changes from spotty to test awesome so that's how you access and change values in a class the next thing that we can do with a class is we can have what is called class methods so we can actually have functions or methods inside of this class that do different things with the data for example we could have a method called void that prints out all the information we have about the animal so we're just going to call this print and it's going to take no arguments and in here we are simply going to write out the animal names we're going to do name plus and then name then we're going to do h plus h and finally happiness plus happiness so now when we call this method over here it should print out all we know about the dark so far but again if we try and do dark dot print it's not there and that's because just like any other variable we have to give our method and access modifier and here we call it public by the way access modifiers are also called protection level or even scope so if you see that we type out dark dot print here it's now there but should we say remove this or change this to a private and then try and compile it's going to say that animal dot print is inaccessible due to its protection level so there we go so now when we hit play there it's going to say names body h6 and happiness of 0.5 cool next up static variable so we've taken a look at the public and static access modifier but already public and private access modifier but what does the static one do so basically all of these variables and methods are bound to this to the instance of the animal that we are going to be creating so here when we are making a dog animal we are creating a new instance of the class animal but we can also have variables and methods that are shared between all instances of the animal class a good example of this would be if we wanted to keep score of how many animals we have so for that we can make a static even a public static so we can access it from outside and we're going to make this of type int and we can simply name this count and default it to zero so what we can do now is basically and we can write dark dot you can see we could change this to public int count and then write dark dot count but this wouldn't make sense because when we say add one on to the dark and then add another animal let's call this one cat and then do cat that count this should return zero so let's try cap dot count so what we're doing here is we are making a dark instance we are upping the count then we are creating a cat instance and we are printing the cats count and they are going to be totally separate you can see that's a zero and if we print out dark count we get a one so in order to eliminate this we use public static int count and then now we access it in a different way we don't do dark dot count instead what we do is we type the name of the class which is animal dot count and then we can directly access it this way so we say that animal the count plus equals one when this case we're making two animals so we're going to say plus equals two and then we can write out dr. we could just write out not dark the count but animal dot count down here and you'll see that it writes out - so now it's shared so that's super cool and but right now we have to kind of increment it every time we are making a another animal and what would be super cool was if it did this itself another super cool thing that we might want to do is feed in a bunch of information for about the animal when we first created instead of just having it default to a bunch of things and then having to go through and do talk that name equals something and and so on and so on so in order to do this we use what is called a class constructor so let's take a look at creating a class constructor for our animal class so firstly we make some room then we type the name of the class which is animal and then we do a open and close parenthesis followed by an opening closed bracket so basically if you notice this looks very much like a method call where a method definition bunt and and in reality it's it's quite a lot a class constructor is very much like a method that is called right the second that we create a new instance of the class and so basically what we can use this for is if we just delete all of the default values up here so we'll make what is called a default class constructor and we'll just do a name equals body H equals R six and happiness equals zero point five F so this is going to function basically the same as what we did up there so when we kind of run this now oops it's inaccessible now that's because we have to make this a public animal it's going to run basically the same but what we can do is we can now create another version of this and that where we can input the values down here and also what we can do is we can simply up the count right in here so we can simply say count plus plus so now what we do is we create an animal called I'm dark and then we create an animal called cat and I want this to be a new animal and I want to give this some values so in order for us to make to do that we simply make another class constructor so I think it's time to create some comments here so we're going to take these class constructors down here you're going to say class methods and up here we're going to say class variables so here we create another constructor call this public animal and open and close parentheses and some curly brackets and in here we then simply give it some arguments just like we've done with method methods so we simply do a string call this underscore name a an int call this underscore age and and and a float call this underscore happiness so the underscore is just there to distinguish it from the public variables up here it's a very common naming convention when it comes to temporary variables like this that are passed through an argument so what we can do now is simply say that our name should equals underscore name our h equals underscore h our happiness equals underscore happiness and count plus plus so now we have two ways of creating an animal the default way and the with using the kind of the constructor that actually has some parameters that we can pass to it and let me simply say we want a new animal we want this to be called um mr. Bean's we want him to have an age of ten and a happiness of are 0.8 and then we can of course take this cat here and we can print out the information we have of it and then at last we can maybe make a new line and followed by the car count of animals so num of animals I'm going to set that equal to animal dot count so when we hit play you can see whoops just to make this pretty I'm going to put in an empty line here too so we can see now that we have a an animal named spotty hf6 happiness of 0.5 another one called mr. Bean's age of 10 happiness of 0.8 and the total number of animals that we have is equal to 2 comme so that was a basic kind of rundown of classes now you know everything you need to know to create 90% of the classes that you are going to be writing and this is so essential for structuring your code reusing code and really opening up the possibilities of kind of creating your own pieces of data that you can then distribute so that's super super cool and if you you got this already well done if not that's completely understandable this can be a lot of information and quite a bit of new syntax but either way I hope you enjoyed the video and I'll see you in the next one
Info
Channel: Brackeys
Views: 301,884
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, model, beginner, easy, function, and, settings, how, to, howto, learn, learning, course, series, tips, tricks, tutorials, workflow, fix, tip, technology, game, development, develop, games, programming, coding, basic, basics, C#, class, classes, access, modifier, modifiers, protection, level, Unity (Software), public, private, static, constructor, constructors, arguments, parameters, initialization, instantiation, init, method, methods, calls
Id: s2hHjpZaSyI
Channel Id: undefined
Length: 18min 58sec (1138 seconds)
Published: Sun May 17 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.