Monday, April 2, 2018

Variable Scope, Overflowing Checked and Unchecked Keywod

Scope

Scope is actually all about accessibility of the variable. The point where we can access it, we can use it, we can read or write the variable. These are the things are actually the scope of the variable. If we define the variable at global level then we can access it in functions as well. Similarly the scope of a procedure and function is defined by the place where we can call that method and where you’re allowed to call that method. This defines the scope of variables and functions.
Here we’ve different levels where we define the variable.

Block Level

If we define the variable at block level then it can be accessible within the block. We can’t access it and use it outside of the block.

Procedure/Function

Same as above if we define the variable in functions then its scope is within the ending the curly braces of function.

Class

If we define the property at class level then its scope is dependent upon the access modifier through which we define the class itself, its properties and methods.

Project

Like if you’ve import the assembly into your project then the properties methods which are public or static can be accessible in that specific object.
We’ll define the access modifiers later on in this series.

Overflowing

Actually we already know that every container has its own limit which it can hold the value, each data type has minimum value and maximum value as well that we can store into it but if we cross the boundary and put the value more than the value that it should have then normally it shows us the minimum least value that it can hold but it don’t throw the exception.
And if we decrement the value more the last digit this datatype can have then we’ll see the maximum last digit it have.
But now this kind of implementation is not so much good. The program should throw the exception in such kind of scenarios. Then we’ve 2 techniques for this purpose.
  • ·        Turn on the compiler settings
  • ·        Use the checked and unchecked keyword
Let’s discuss them one by one

Compiler Settings

  • ·        Paste this code in Visual Studio
 static void Main(string[] args)  
 {  
   int a = int.MaxValue;  
   Console.WriteLine("Max Value of int is {0}", a);  
   int b = ++a;  
   Console.WriteLine("Max Value of int after Increment {0}", b);  
   Console.ReadLine();  
 }  
  • ·        Now right click on the project and select properties
  • ·        In Build Tab Click on Advanced Button and mark the checkbox
  • ·        Now run the application. And you’ll see the exception.

Checked Keyword

  • ·        Now uncheck the checkbox and just paste the code into the checked block.

 static void Main(string[] args)  
 {  
   checked  
   {  
     int a = int.MaxValue;  
     Console.WriteLine("Max Value of int is {0}", a);  
     int b = ++a;  
     Console.WriteLine("Max Value of int after Increment {0}", b);  
   }  
   Console.ReadLine();  
 }  
  • ·        Now run the application and now our program is throwing exception.
So this is how we can use these kind of things in our program to know that something is wrong happening there.

Unchecked Keyword

Unchecked keyword actually allow us and prevent us from the exceptions when the developer make sure about the possible results then he applies unchecked block on the code.
Look when we’re using const keyword with the datatype then it doesn’t matter whether you’re changing this value or not but even you can’t assign the value to any container by incrementing this constant value.

In such kind of scenarios, we need to use unchecked keyword here.


0 comments:

Post a Comment