Code blocks in programming are essential for grouping code lines and controlling variable accessibility. Variable scope, which determines where a variable can be accessed, is influenced by code blocks.
Code blocks play a crucial role in determining the scope of variable declarations. Variable scope refers to the visibility of a variable within your application’s code. A variable declared within a code block is locally scoped, meaning it is accessible only within that specific block. Attempting to access the variable outside the block will result in a compiler error.
To begin, create a static class file called “CodeBlocksAndScope.cs” within the console application. Insert the provided code snippet into this file.
/// <summary>
/// Output
/// Inside the code block: 10
/// </summary>
public static void VariableInCodeBlock()
{
bool flag = true;
if (flag)
{
int value = 10;
Console.WriteLine($"Inside the code block: {value}");
}
}
Execute the code using the main method as follows.
#region Day 2 - Variable Scope & Logic Control with Code Blocks
CodeBlocksAndScope.VariableInCodeBlock();
#endregion
// Console Output
Inside the code block: 10
Add another method into the same static class wherein the code attempts to access the variable outside the code block.
/// <summary>
/// Outputs
/// Program.cs(7,46): error CS0103: The name 'value' does not exist in the current context
/// </summary>
public static void VariableOutCodeBlock()
{
bool flag = true;
if (flag)
{
int value = 10;
Console.WriteLine($"Inside the code block: {value}");
}
//Uncomment below line to validate
//Console.WriteLine($"Outside the code block: {value}");
}
Execute the code from the main method as follows.
#region Day 2 - Variable Scope & Logic Control with Code Blocks
CodeBlocksAndScope.VariableOutCodeBlock();
#endregion
// Console Output
Program.cs(7,46): error CS0103: The name 'value' does not exist in the current context
This error is generated because a variable that’s declared inside a code block is only accessible within that code block.
Add another method into the same static class wherein the code attempts to access the variable, i.e., declared above the code block, but it is not initialized.
/// <summary>
/// Outputs
/// Program.cs(6,49): error CS0165: Use of unassigned local variable 'value'
/// </summary>
public static void VariableAboveCodeBlock()
{
bool flag = true;
int value;
if (flag)
{
//Uncomment below line to validate
//Console.WriteLine($"Inside the code block: {value}");
}
value = 10;
Console.WriteLine($"Outside the code block: {value}");
}
Execute the code using the main method as follows.
#region Day 2 - Variable Scope & Logic Control with Code Blocks
CodeBlocksAndScope.VariableAboveCodeBlock();
#endregion
// Console Output
Program.cs(6,49): error CS0165: Use of unassigned local variable 'value'
Add another method into the same static class wherein the code attempts to access the variable i.e., declared above the code block, and assign a value
/// <summary>
/// Outputs
/// Inside the code block: 0
/// Outside the code block: 10
/// </summary>
/// <returns></returns>
public static void VariableAboveCodeBlockv1()
{
bool flag = true;
int value = 0;
if (flag)
{
Console.WriteLine($"Inside the code block: {value}");
}
value = 10;
Console.WriteLine($"Outside the code block: {value}");
}
Execute the code from the main method as follows
#region Day 2 - Variable Scope & Logic Control with Code Blocks
CodeBlocksAndScope.VariableAboveCodeBlockv1();
#endregion
// Console Output
Inside the code block: 0
Outside the code block: 10
GitHub - ssukhpinder/30DayChallenge.Net
Thank you for being a part of the C# community! Before you leave:
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter Visit our other platforms: GitHub | Instagram More content at C# Programming
Also published here.