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

  • SEARCH
  • Home
  • 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 8328975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:36:56+00:00 2026-06-09T01:36:56+00:00

I have the following code inside a method: string username = (string)context[UserName]; string un

  • 0

I have the following code inside a method:

string username = (string)context["UserName"];

string un = (string)context["UserName"];

The problem is that the first string “username” is not assigned, while the second does.

To make it more strange, when I have stopped the debugging after the first line and copied the line to the Immediate window, dropping the varible type delaration, it was assigned succesfully.

I have made rebuild all and checked project properties which seems to be OK.

The context variable is a System.Configuration.SettingsContext, which is a hash table. To be more specific, I’m implementing a profile provider, the GetPropertyValues method.

I am using VS 2012 and .NET 4.5

EDIT:

I am using code contract in my project, which uses compile time code injection for runtime checking. I disabled it and all is working well. I’ll try to remove contracts one by one to find which one is causing the problem.

  • 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-09T01:36:58+00:00Added an answer on June 9, 2026 at 1:36 am

    What you are seeing is similar to a Code Contracts bug I saw before. I wrote something about it here a few months back. If you have this bug, you probably also have a lambda or LINQ expression in your method that uses username.

    For future reference, this is the bug I saw:

    I had in the same method a lambda expression capturing a local variable, lets say values, and a Contract.Requires() expression checking something completely unrelated. While debugging that method, the debugger shows in Locals the variable values twice, and reports the value of values always as null even when this is clearly not the case.

    To reproduce:

      static void Reproduction(string argument)
      {
          Contract.Requires(argument != null); // <-- (1)
    
          int[] values = new int[1];
          Debug.Assert(values != null);
    
          Func<int, bool> d = i => values[i] >= 0; // <-- (2)
    
          Console.WriteLine(values);
      }
    

    Put a breakpoint somewhere in this method after the assignment to values and ensure that the method gets called. When the debugger hits the breakpoint, look at the Locals list of Visual Studio for the duplicate variable. Hover the mouse over values to find that it gets reported as being null, something which is clearly not the case.

    The problem disappears when removing either the contract (1) or the line with the lambda (2).

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

Sidebar

Related Questions

I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have the following method I came across in a code review. Inside the
I have the following code that is supposed to find an element inside of
I have the following method in some nemerle code: private static getLinks(text : string)
I have the following code inside a loop: Range(N & i + 1).Formula =
I have the following code with a bool if statement inside and in another
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
I have following code for loading image from url in xml parsing endElement method
i have the following model method inside my asp.net MVc web application:- public IQueryable<User>
I have the following code that does something very simple: visits recursively a tree

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.