Cry about...
.NET / C# Troubleshooting

The name 'NNNN' does not exist in the current context (C#)


When compiling a C# application the compiler generates the following error:

The name 'NNNN' does not exist in the current context

where 'NNNN' is the name of a variable.

If you are using VB.Net then the error message is slightly different (but means the same thing):

'NNNN' is not declared. It may be inaccessible due to its protection level.

in this case please refer to the VB.Net version of this article "NNNN is not declared".

Possible Cause 1: (see also Possible Cause 2 below)

The compiler does not recognise the variable 'NNNN'. This is either because the variable name is misspelt or because the compiler could not find a corresponding definition.


  • A common cause is a simple misspelling where the identifier is used. For example:
int number;
numbr = 1;

Here the identifier is defined as 'number' but used in the code as 'numbr'. The solution is to correct the spelling.

  • If the name is referring to an identifier then it may be that the reference simply needs to be qualified. For example:
using System.Web.HttpContext;

class Example
    string ExampleFunc()
        return Application["name"];
[Error] Name 'Application' is not declared.

Try replacing "Application" with "Current.Application".

  • The most common cause is that the namespace that defines the name is missing. Identify and import the required namespace - the table below should help.

    For example, with the error:

    The name 'Directory' does not exist in the current context

    The missing namespace is System.IO, so the solution is to add:

    using System.IO;

    at the top of the file.

    The following table (which is not exhaustive) lists identifiers together with the namespace and any qualification that might typically be required:

    Name Namespace Common Qualified Name
    Abs Math Math.Abs
    AnchorStyles System.Windows.Forms  
    Application System.Windows.Forms
    For Windows forms applications.
    For web applications.
    ApplicationDeployment System.Deployment.Application  
    Assembly System.Reflection  
    BindingFlags System.Reflection  
    CultureInfo System.Globalization  
    Cache System.Web.Caching  
    CipherMode System.Security.Cryptography  
    ConfigurationManager System.Configuration
    Also ensure that project contains reference to System.Configuration.dll
    Current System.Web HttpContext.Current
    Debug System.Diagnostics  
    Debugger System.Diagnostics  
    Directory System.IO  
    Dns System.Net  
    Encoding System.Text  
    EventLog System.Diagnostics  
    File System.IO  
    FormsAuthentication System.Web.Security  
    HostingEnvironment System.Web.Hosting  
    HtmlEncode System.Web.HttpUtility  
    HttpContext System.Web  
    HttpRuntime System.Web  
    HttpStatusCode System.Net  
    HttpUtility System.Web  
    ImageFormat System.Drawing.Imaging  
    Marshal System.Runtime.InteropServices  
    NormalizationForm System.Text  
    PaddingMode System.Security.Cryptography  
    Parallel System.Threading.Tasks  
    Path System.IO  
    Process System.Diagnostics  
    Regex System.Text.RegularExpressions  
    Request System.Web HttpContext.Current.Request
    Round Math Math.Round
    SecurityElement System.Security  
    SecurityMode System.ServiceModel  
    SecurityZone System.Security  
    Server System.Web HttpContext.Current.Server
    ServicePointManager System.Net  
    SmptMail System.Web.Mail  
    SslPolicyErrors System.Net.Security  
    Stopwatch System.Diagnostics  
    ThreadPool System.Threading  
    UnicodeCategory System.Globolization  
    UrlDecode   HttpContext.Current.Server.UrlDecode
    User   HttpContext.Current.User
    UTF8Encoding System.Text  
    VirtualPathUtility System.Web  

    It is my intention to add to this table over time.

Possible Cause 2:

A less likely cause is that the project itself lacks a reference to the necessary library. This can arise when (for example) creating a class library and using web namespaces, for example:

The name 'HttpRuntime' does not exist in the current context


The name 'HttpContext' does not exist in the current context

even though the line "using System.Web" is included.


Add the reference to the project:

  1. Expand the "References" shown in the solution explorer for the project.
  2. Check that the required namespace is listed under "References".

    The required reference is listed in the table above (given under "Possible Cause 1"). If it is not listed then right click on "References" and select "Add reference...". In the "Add Reference" dialog box that will then appear, the reference you require will probably be on the ".NET" tab.

For example, the solution to "HttpRuntime (or HttpContext) does not exist" (even though "Imports System.Web" is included in the file) is to ensure that "System.Web" is listed as one of the project References.

These notes are believed to be correct for C# for the .NET 4, .NET 3, .NET 2 and .NET 1.1 frameworks, and may apply to other versions as well.

For the corresponding VB.NET version of this article please see "NNNN is not declared".

About the author: is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.

The best mobile software on Phones Spy is SpyBubble .