0

I'm working on a legacy ASP.NET website in Visual studio 2008. The project builds on some other system but it is throwing The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) error in my machine.I suspect my visual studio is compiling the code in C# 2.0.

I know there are options in Visual studio latest version to change the C# version but such options are not available in visual studio 2008. How to change the C# version in Visual Studio 2008 Website.

VS 2008 Build options

Build options of the project

Jay
  • 41
  • 7
  • 2
    There have always been options for which C# version to use - in the Build tab, it's in the "advanced" dialog, I believe. (I can't easily test that, as I'm not about to install VS2008... is there any reason you can't use a more modern version of VS for the legacy project?) – Jon Skeet Jul 31 '20 at 06:52
  • Having said that, if the project builds on a different system, that suggests it's *not* a project-specific issue. If you start an entirely new console app in a new solution on the broken system, does that still fail with `var`? – Jon Skeet Jul 31 '20 at 06:53
  • @JonSkeet When i create a new project I can use the var. Question edited. Added the build options of VS 2008 – Jay Jul 31 '20 at 06:56
  • No, not the build menu - the build properties of the project. Right-click on the project, and click "Properties". – Jon Skeet Jul 31 '20 at 06:59
  • (That said, "web site" projects have always been a bit odd in my experience, vs regular ASP.NET projects.) – Jon Skeet Jul 31 '20 at 06:59
  • Hmm. Yeah..Websites are add. However adding the screenshot of the project properties. Any help will be appreciated. – Jay Jul 31 '20 at 07:02
  • As far as i know, you need to add `using System.Linq` directive in the class where the error exists. Can you confirm? – Maciej Los Jul 31 '20 at 07:09
  • This might be helpful too: [The type or namespace name 'var' could not be found in WCF Service Application](https://stackoverflow.com/questions/5337421/the-type-or-namespace-name-var-could-not-be-found-in-wcf-service-application). See the answer provided by @bkaid. – Maciej Los Jul 31 '20 at 07:12
  • @MaciejLos: No, you absolutely don't need the `System.Linq` namespace for var. – Jon Skeet Jul 31 '20 at 07:12
  • @Jay: I'd look under "MSBuild Options" there - it's just *possible* there's something in there. I'd also try to build it from the command line, to isolate things a bit further. – Jon Skeet Jul 31 '20 at 07:13
  • @JonSkeet, i suspect that `var` is used to get `IEnumerable`. In that case `System.Linq` is required. Without seeing the code, it's just a guess. – Maciej Los Jul 31 '20 at 07:19
  • @Jay, can you improve your question and add a piece of code which causes the error? – Maciej Los Jul 31 '20 at 07:20
  • 1
    @MaciejLos: No, it's really not required. This error could *not* be the result of a using directive being missing, unless there's genuinely a type named `var` in a different namespace. If `System.Linq` being missing were the culprit, the error message would be very different - the failure would be at the call site, not assigning the result of that call to a new variable. If you doubt this, please try to come up with a complete example that gives this error, but which can be fixed with `using System.Linq;`. – Jon Skeet Jul 31 '20 at 07:28
  • System.Linq is being used in the class – Jay Jul 31 '20 at 07:30
  • 1
    @JonSkeet, Thank you for explanation. I'm impressed of your knowledge. I've seen your blog, read your articles (almost all) and a book "C# in Depth". As @Jay mentioned, `System.Linq` is used, so... i was wrong. Best Regards, Maciej. – Maciej Los Jul 31 '20 at 07:46

1 Answers1

0

According to the article C# language version history, C# 3.0 released with .NET 3.5 and VS2008 and it contains implicit typing (var).

Therefore, you don't have to change the c# version.

As usual, the error The type or namespace name 'var' could not be found will occur when we use the code like the following.

public class Foo
{
    var a = 4;
}

We should change it into :

public class Foo
    {
        public static void SayHello()
        {
            var a = 4;
        }

    }

Because variables that are declared at method scope can have an implicit type var.

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27