Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:36:19+00:00 2026-06-17T04:36:19+00:00

Consider the following source: static void Main(string[] args) { bool test; Action lambda =

  • 0

Consider the following source:

static void Main(string[] args)
{
    bool test;

    Action lambda = () => { test = true; };
    lambda();

    if (test)
        Console.WriteLine("Ok.");
}

It should compile, right? Well, it doesn’t. My question is: according to C# standard, should this code compile or is this a compiler bug?


The error message:

Use of unassigned local variable 'test'

Note: I know, how to fix the error and i partially know, why does it happen. However, the local variable is assigned unconditionally and I guess, that compiler should notice that, but it does not. I wonder, why.


Comment for answers: C# allows declaring unassigned variables and that’s actually quite useful, ie.

bool cond1, cond2;
if (someConditions)
{
    cond1 = someOtherConditions1;
    cond2 = someOtherConditions2;
}
else
{
    cond1 = someOtherConditions3;
    cond2 = someOtherConditions4;
}

Compiler compiles this code properly and I think, that leaving variables unassigned actually makes the code a little bit better, because:

  • It tells the reader, that values are assigned later (mostly probably in the following conditional statement)
  • Forces the programmer to assign the variables in all branches of internal conditions (if it was the purpose of this code from the beginning), because compiler will refuse to compile the code if one of the branches does not assign one of them.

On the margin:
That’s even more interesting. Consider the same example in C++:

int main(int argc, char * argv[])
{
    bool test;

    /* Comment or un-comment this block
    auto lambda = [&]() { test = true; };
    lambda();
    */

    if (test)
        printf("Ok.");

    return 0;
}

If you comment the block out, compilation ends with warning:

main.cpp(12): warning C4700: uninitialized local variable 'test' used

However, if you remove the comment, compiler emits no warnings whatsoever. It seems to me, that it is able to determine, if the variable is set after all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:36:20+00:00Added an answer on June 17, 2026 at 4:36 am

    My question is: according to C# standard, should this code compile or
    is this a compiler bug?

    This is not a bug.

    Section 5.3.3.29 of the C# Language Specification (4.0) outlines the definite assignment rules regarding anonymous functions, including lambda expressions. I will post it here.

    5.3.3.29 Anonymous functions

    For a lambda-expression or anonymous-method-expression expr with a body (either block or
    expression) body:

    • The definite assignment state of an outer variable v before
      body is the same as the state of v before expr. That is, definite
      assignment state of outer variables is inherited from the context of
      the anonymous function.

    • The definite assignment state of an outer variable v after
      expr is the same as the state of v before expr.

    The example

    delegate bool Filter(int i);
    
    void F() {
        int max;
    
        // Error, max is not definitely assigned    
        Filter f = (int n) => n < max;
    
        max = 5;    
        DoWork(f); 
    }
    

    generates a compile-time error since max is not definitely assigned
    where the anonymous function is declared. The example

    delegate void D();
    
    void F() {    
        int n;    
        D d = () => { n = 1; };
    
        d();
    
        // Error, n is not definitely assigned
        Console.WriteLine(n); 
    }
    

    also generates a compile-time error since the assignment to n in the
    anonymous function has no affect on the definite assignment state of n
    outside the anonymous function.

    You can see how this applies to your specific example. The variable test is not specifically assigned prior to the declaration of the lambda expression. It is not specifically assigned prior to the execution of the lambda expression. And it is not specifically assigned after the completion of the lambda expression execution. By rule, the compiler does not consider the variable to be definitely assigned at the point of it being read in the if statement.

    As for why, I can only repeat what I have read on the matter, and only what I can remember as I cannot produce a link, but C# does not attempt to do this because, although this is a trivial case that the eye can see, it is far more often the case that this type of analysis would be non-trivial and indeed could amount to solving the halting problem. C# therefore “keeps it simple” and requires you to play by much more readily applicable and solvable rules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following code: public sealed class Program { public static void Main() { System.Console.WriteLine(Hi);
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
Consider the following source file, which is (at least should be) valid C. void
Ok, this is a weird one. Consider the following: <audio id=background_audio autoplay=autoplay> <source src=static/audio/clip.ogg
Consider the following data source: declare @Test table (EmpId int, ProdId int, Sold int)
Consider following source, reduced for simplicity int main() { int d[2]; pipe(d); if(fork()) {
Consider the following test case: #define _GNU_SOURCE #include <stdio.h> #include <stdarg.h> void test(char **outa,
Consider the following header and source files: // main.cpp #include myClass.h int main() {
Consider the following Pattern :- aba And the foll. source string :- abababbbaba 01234567890
Consider the following C99 function: void port_pin_set(const bool value, const uint8_t pin_mask) { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.