C# Programming Tutorial 31 – Variable Scope with Nested Control Flow

yo yo yo what's going on everybody this video what are we gonna be talking about we're going to be talking about variable scope at least the basics of it just to get everybody on the same page as we start getting into more complex applications so before we get started please check out our sponsor Monday comm is your visual project management solution this is the tool that allows you to see where every task or project stands with a single glance with a fully customizable interface you can create the exact workflow that you need for you and your team to get stuff done Monday comm is available on mobile and integrates well with some of the most popular tools out there so get your life in order by giving it a try for free link in the description alright so here is a situation where we are getting a a naming conflict essentially because we declare this I variable up here and then down here we declare again and the reason we're getting this error here is because the other one is still in scope so c-sharp is not going to let us create it so what is the solution of this well instead of declaring I again we could just give it the value 9 so we can reassign it to value 9 by getting rid of the datatype prefect's so that's how you assign a value to a variable without declaring it or we could use a different variable name that's totally up to you how you want to fix it but in this situation I actually don't even need this for loop at all so I'm just gonna delete it you know when it doesn't work just delete it that's what I do alright so when we create a variable its scoped to the curly braces it's defined in so if we created a method down here we can use the I variable this I is only scoped to this block right here so outside of that block I is going to be allowed to be used so if we create a variable inside of a block such as K here we're declaring we're not going to be able to use that variable outside of the curly braces it's defined in so in this situation we can't use K here if we do a right line and try to print k it says the name k does not exist in the current context so because we're outside of that scope that K is defined in we could read eclair k and this time it's going to be a different k the whole concept of variable scopes probably going to come up again when we get into methods because variables created inside of methods are not going to be the same variables created outside of the method call so what that means is this right method for example it might have a variable inside called K and we're not overriding that value or anything like that but we'll get into that in detail when we talk about methods once we get into object oriented programming in this series now another thing is global variables all programming languages have some concept of a global variable or something very similar so for example in c-sharp inside of the class we can create a class variable we can say int X and give it the value 5 you'll see class variables when we're creating our own custom classes you may also hear fields very similar to properties but we're getting into the the naming stuff so we're not gonna worry about it essentially this variable is more likely to cause naming issues than a variable scope to a tighter curly brace or a tighter block so only create variables where you absolutely need them if you need this variable throughout the entire class that's totally fine go ahead and put it there but just be aware that increases the chance for naming conflicts to see what I mean with these class variables we can go down here past the static void main method so right here and paste this method I wrote earlier hovering over it says local variable X hides field X so you can see how we're more likely to come in to issues when we put our variables in a very large scope so that's all I got for you guys on the basics of scoping we're gonna get into that more as we gain 200p I'm pretty stoked so be sure to subscribe because we're gonna be getting to that pretty soon we're just gonna take a little break to talk about collections a couple other things and then we'll be object or in programming we here but sooner than you know so be sure to subscribe and I'll see you soon

Tags: , , , , , , , , , , , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *