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 7991903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:25:29+00:00 2026-06-04T13:25:29+00:00

string foo; try { foo = test; // yeah, i know … } catch

  • 0
string foo;
try
{
    foo = "test"; // yeah, i know ...
}
catch // yeah, i know this one too :)
{
    foo = null;
}
finally
{
    Console.WriteLine(foo); // argh ... @#!
}
Console.WriteLine(foo); // but nothing to complain about here

Besides it’s not BP (catching-routing) – but this is the best isolation I can get.
But I get nice waves telling me “danger, danger – might be uninitialized”.
How comes?

Edit:
Please do not suggest “Simply put a string foo = string.Empty; at the ‘declaration'”. I’d like to declare it, but just do the assignment on time!

  • 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-04T13:25:31+00:00Added an answer on June 4, 2026 at 1:25 pm

    Some background from the C# specification (5.3.3.14):

    For a try statement stmt of the form:

    try try-block finally finally-block

    (…)

    The definite assignment state of v at the beginning of finally-block
    is the same as the definite assignment state of v at the beginning of
    stmt.

    Edit Try-Catch-Finally(5.3.3.15):

    Definite assignment analysis for a try-catch-finally statement (…)
    is done as if the statement were a try-finally statement enclosing a
    try-catch statement

    The following example demonstrates how the different blocks of a try
    statement (§8.10) affect definite assignment.

    class A
    {
      static void F() 
      {
        int i, j;
        try {
          goto LABEL;
          // neither i nor j definitely assigned
          i = 1;
          // i definitely assigned
        }
        catch {
          // neither i nor j definitely assigned
          i = 3;
          // i definitely assigned
        }
        finally {
          // neither i nor j definitely assigned
          j = 5;
          // j definitely assigned
        }
        // i and j definitely assigned
        LABEL:;
        // j definitely assigned
      }
    }
    

    I just thought of an example that shows the problem better:

    int i;
    try
    {
        i = int.Parse("a");
    }
    catch
    {
        i = int.Parse("b");
    }
    finally
    {
       Console.Write(i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For this example Java class: package foo; public class TestInterop { public String test(int
I'd like to write a doctest like this: >>> print a.string() foo : a
void Foo() { string ID = test; var testctrl = new Control() {ID =
This one just stabbed me hard. I don't know if it's the case with
I try to modify /foo/bar/dir to \/foo\/bar\/dir by ruby gsub command. I test it
int main(void) { std::string foo(foo); } My understanding is that the above code uses
I have a method String Foo<T> where T: WebControl Now I do have a
Take the following property: public string Foo { get; private set; } Using reflection,
I have the following string which will probably contain ~100 entries: String foo =
I would like to convert this string foo_utf = u'nästy chäräctörs with å and

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.