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

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}");
    }
}        

Execute the code from the main method as follows

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

CodeBlocksAndScope.VariableInCodeBlock();

#endregion        

Console Output

// Console Output
Inside the code block: 10        

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}");
}        

Execute the code from the main method as follows

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

CodeBlocksAndScope.VariableOutCodeBlock();

#endregion        

Console Output

// 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.

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}");
}        

Execute the code from the main method as follows

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

CodeBlocksAndScope.VariableAboveCodeBlock();

#endregion        

Console Output

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

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}");
}        

Execute the code from the main method as follows

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

CodeBlocksAndScope.VariableAboveCodeBlockv1();

#endregion        

Console Output

// Console Output
Inside the code block: 0
Outside the code block: 10        

Complete Code on?GitHub

GitHub - ssukhpinder/30DayChallenge.Net Contribute to ssukhpinder/30DayChallenge.Net development by creating an account on GitHub.github.com


Clap if you believe in unicorns & well-structured paragraphs! ????

Follow for more updates on

C# Publication | LinkedIn | Instagram | Twitter | Dev.to

要查看或添加评论,请登录

社区洞察

其他会员也浏览了