DEV Community

Cover image for Day 2 of 30-Day .NET Challenge: Variable Scope & Logic Control with Code Blocks
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

Day 2 of 30-Day .NET Challenge: Variable Scope & Logic Control with Code Blocks

Introduction

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.

Learning Objectives:

  • Gain insight into the implications of declaring and initializing variables within and outside code blocks.

Prerequisites for Developers

  • Declaring and initializing variables.

  • Utilizing if-else selection statements.

  • Employing foreach iteration statements.

  • Calling methods from classes within the .NET Class Library.

Getting Started

How do code blocks impact variable scope?

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.

Declare a variable inside the code block

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}");
    }
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 2 - Variable Scope & Logic Control with Code Blocks

CodeBlocksAndScope.VariableInCodeBlock();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
Inside the code block: 10
Enter fullscreen mode Exit fullscreen mode

Access a variable outside the code block

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}");
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 2 - Variable Scope & Logic Control with Code Blocks

CodeBlocksAndScope.VariableOutCodeBlock();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
Program.cs(7,46): error CS0103: The name 'value' does not exist in the current context
Enter fullscreen mode Exit fullscreen mode

This error is generated because a variable that’s declared inside a code block is only accessible within that code block.

Declare a variable unassigned above the code block & access inside the 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}");
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

#region Day 2 - Variable Scope & Logic Control with Code Blocks

CodeBlocksAndScope.VariableAboveCodeBlock();

#endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

// Console Output
Program.cs(6,49): error CS0165: Use of unassigned local variable 'value'
Enter fullscreen mode Exit fullscreen mode

Declare a variable assigned above the code block & access inside the block

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}");
}
Enter fullscreen mode Exit fullscreen mode

Execute the code from the main method as follows

    #region Day 2 - Variable Scope & Logic Control with Code Blocks

    CodeBlocksAndScope.VariableAboveCodeBlockv1();

    #endregion
Enter fullscreen mode Exit fullscreen mode

Console Output

    // Console Output
    Inside the code block: 0
    Outside the code block: 10
Enter fullscreen mode Exit fullscreen mode

Complete Code on GitHub

GitHub - ssukhpinder/30DayChallenge.Net

C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter
Visit our other platforms: GitHub | Instagram
More content at C# Programming

Top comments (0)