 Hello guys crypto grounds here welcome back to another C sharp crash course video this episode 5 and today We're going to be doing constructors So last video we talked about all the variables that we can do and this is pretty important when we create our constructors for our classes Yeah constructors, okay If you enjoyed this video and if you learned something new make sure you smash that like button subscribe my channel If you're new and turn on those notifications if you want to catch up on the latest content and live streams Comment all your feedback Compliments suggestions all that good stuff below and if you want to support me check out the patreon link is in the description And let's get on with it. So I'm gonna do all this Okay, so we have our State namespace in our state class and our blueprints namespace with our district class as well So we're gonna set up our district class because we want to make this into an object that we can use to define What a district is okay? So a constructor is so what is a constructor a constructor is basically a way to pre-define or pre-initialize some variables or whatever our Default variables in our class is when we initialize an object such it when we do new It's here. Let's create this constructor first. Okay, and I'll show you how it all works So the constructor is simply just public district. Okay now each class has a Has a hidden Constructor inside it. That's why your variable stuff is zero or your Boolean says false It's an invisible constructor, but we can change that by adding or by overriding the District constructor. Okay, so we do it by typing public a district and our parentheses with the curly braces like that Okay, so let's have a variable. So let's do a string I'm gonna do public variable for now, but we're gonna call this a district name. Okay So we have a variable here, but by default it's just gonna be an empty string until we change it in here So let's put a parameter Let's do district name in here. Okay, and in order to Def Have a default definition for what to this variable is we do name is equal to district name Which is the parameter now what I what I like to do is have the variables matching. Okay, so you do this by just having the same name and Except we added this Key word to the beginning of the what we're setting equal to and now people now People tend to forget Where this this actually goes people will think that name is equal to this dot name because this is like it's talking about this Which completely makes sense, but we're talking about this one right here So we're going to be setting it to here and we're talking about this class's object That's where this is coming from and one way to remember is that whenever we're setting a Variable like for the outside That goes on the left hand right so we're setting it to the right hand side, which is our parameter right here, okay? So like that so now when we create our district object right here we type in the new keyword and District and we add our parentheses in here now. We can't have an empty constructor in here But it won't do any good and actually it has a parameter in here So if we just get rid of all this right here We can do this right it creates a new object and it has an empty constructor So this is completely valid. However, we have a constructor with one parameter right here So give us an error constructor district has one parameters, but is invoked with zero arguments So we need so the zero arguments in here So we need to put in arguments that matches our parameter Okay, and I'll talk more about parameters and arguments in a future video, but basically all methods and Constructors all this good stuff are gonna need it. So we're gonna put in our custom string, which is gonna be I don't know, California. I Don't I don't remember. I'm just gonna put California School district or something like that Okay, so now our arguments for our parameter name is California school district. So when we when we initialize this We have our district object right here and We created a district with the name California school district. Okay Let's add another variable and also this is gonna be able to tell us to that we can make this field private However, I'm not gonna do that right now. So we're gonna make a We're gonna make a u short. Remember what are you short is it's an unsigned short basically an unsigned 16-bit integer. Okay, so this is positive only we're not gonna have so this variable is gonna be students or actually this could be We can just do this a bite And this would be schools, right? I mean, obviously we can have a million schools, but I don't know any district that has more than 255 schools. So we'll just do assigned bites, which is that we're not gonna do I know we're gonna unsigned bites. Okay, we want it to be positive only so we're not gonna do s bites We're gonna be unsigned bites. So that's just bite. Okay, and This could just be called school count And in here, I'm gonna do bite schools. Okay now again, you could be consistent You could be inconsistent however you want, but I'm just showing you as an example Okay, so we're gonna set a school count to the parameter schools And now we're gonna have another red line here because we need to add a Bites variable in here. So how many schools do we want our district? Let's put 25 Okay, so now we created a district that is called California school district and it has 25 schools in it and not literally but that's just according to our definition now another thing we can do here is we can override these These constructors, okay, so you can create constructors with certain parameters, right? So let's copy and paste this now. This is the exact same thing. So we're gonna get a red error here So let's say we don't know what how many school this is gonna have yet We can simply get rid of this right here. And now we created a second Constructor with only the name of parameter. Okay, we can get rid of this Let's say we want the school count to be zero in this one and honestly by default it will be zero So we can pretty much get rid of this, but I'm just gonna keep it in here. So you know how control how this is working out And let's create another So I'm gonna call this district two and you're gonna do a new district and then now we only have the name So we're gonna call this Washington States school again, I don't even know if these are real districts. I'm just making these up Washington state school district. Okay, just for it's just for example so now We have created a district with the name of Washington state school district and we didn't put in an argument for schools because we didn't need to And by default is zero. So our school count is zero. It's an empty district, right? So now we can use either one of these right and you can create as many of these as possible as long as the Parameters there are the type of parameters and how many they have are different. Okay. How many they have okay? You can literally just do like In schools in here and this will this will work too, right? It can be a different variable type But it's just got to be Different from the other right So we're just gonna do it like that. Okay, so let's uh run this I want to show you guys how to do this now the reason why I'm adding static to this is because we are creating We're trying to reference a static variable in a static method Which is public static void main and I'll explain what this is in a future video But this this is where our code starts right here public static void main now This can't be non static. It has to be static Which means in order to access our our variables here our objects. They must be static. Okay, so let's create um A variable right here. It's non static and again. I will get to Static and non static in the future We're gonna just call this a number I guess Okay, so if we try to access this number in the static method, it won't work Okay, so it says um that we cannot access a non static field number in static context, which is this Now if we add the static keyword, we are able to do that And what's also cool is that if we create a non static method For example run for example Or add We can access a static variable inside a non static reference Okay And we can also do non static in a non static reference. We just yeah like that Hopefully that makes sense. So this is completely valid, but we're not going to worry about that now Okay, so make sure these are static Okay Unless or otherwise we would be accessing in a non static reference Or a non static method. Okay, so now we're going to print what the school name is. Okay, so we can do district dot name and We can add and again, I'm going to show you string interpolation in the future Which means we can do cool stuff with our strings and we're going to do our school count So we can do district dot A school count. Okay, and then we're going to add another string right here We're going to call this students or schools Okay And we're going to do the same thing for our second district Okay, so california school district has 25 schools, which we have defined here And washington state school district has zero schools, which we defined here as well. So If you didn't know Adding variables is done like this. Okay, so you have a string variable here, right? And you also add another string with a colon and a space in it And then we add our variable here, which is um A byte now you can do do a two string here But it's redundant because this is already being added by two strings So and a string plus a variable will just automatically cast as a string Okay So yeah, also another thing is that we can make this We can simplify this in the future. We're going to do this in another video It's going to be called the two string method Okay, so it's going to be really cool and very helpful where we can literally just like do district dot two string It'll print out whatever we want. Okay. It can print out name school counts or whatever data that we have inside the class Anyways, if you do this video and you learn something new make sure you smash that like button Subscribe to my channel if you're new and turn on those notifications to get notified for future videos and live streams if you Want to support me makes you check out the patreon. It's the links in the description and if you want to Comment all your suggestions feedback and compliments Make sure you do that. I'll be sure to check those out. Anyways, thank you guys for watching and I'll see you guys in the next one. Peace