jaysonragasa
jaysonragasa

Reputation: 1076

Why this Lambda function won't work in ASP.NET

Dictionary<string, int> myList = new Dictionary<string, int>();
List<KeyValuePair<string, int>> result = new List<KeyValuePair<string, int>>(myList);
result.Sort((first, second) => second.Value.CompareTo(first.Value));

it throws 5 errors on line 3 while building

here's the screenie shottie from ASP.NET 2.0 alt text http://img696.imageshack.us/img696/3668/lambdas.jpg

this is from the Console app in .NET 2.0 alt text http://img138.imageshack.us/img138/4788/lambda2.jpg

so what do you think went wrong?

for John alt text http://img705.imageshack.us/img705/3618/lambda3.jpg

ok.. so here's a RAR file which contains a Console app and a Web app written for .NET 2.0 Lambda.rar

Upvotes: 0

Views: 872

Answers (4)

John Saunders
John Saunders

Reputation: 161831

Just to try to get this in one place:

The C# programming language is largely independent of the .NET Framework. One example of this is that Visual Studio 2008 introduced version 3 of the C# programming language, which supported lambda expressions. That same version of Visual Studio 2008 also introduced version 3.5 of the .NET Framework. It also introduced the ability to target either version 2.0, 3.0 or 3.5 of the Framework, while allowing you to use version 2.0 or 3.0 of the language.

This allows you, for instance, to use C# 3.0 features in a program that targets version 2.0 or 3.0 of the .NET Framework.

Somehow, your ASP.NET application (or is it a web site) is set to use version 2.0 of the C# programming language. Your Console application is set to use version 3.0. That is why it works in your console application and not in your ASP.NET application.

ASP.NET has always, and will always, support the same .NET Framework and C# programming language features as a Console application. If you're seeing a difference between the two, then it's a difference in your settings, not a difference in the platforms. This is based on my knowledge of ASP.NET since the betas of version 1.0.

Upvotes: 3

Dan Puzey
Dan Puzey

Reputation: 34218

Your second screenshot crops off the top of the file (the Using directives), and you explicitly mention ".NET 2" whether by accident or design. So, the obvious question:

Are you using C# 3? Because Lamdas weren't available in C#2. (Note: it's the language version and not the /NET framework version that matters!)

Multi-edit: Damn, my first edit made me even more incorrect :-)

Upvotes: 2

Martin Milan
Martin Milan

Reputation: 6390

using System;
using System.Collections.Generic;

namespace SO
{
    class Program
    {
        public static void Main(string[] args)
        {
            var myList = new Dictionary<string, int>();
            myList.Add("a",2);
            myList.Add("b",50);
            myList.Add("c",6);

            var result = new List<KeyValuePair<string, int>>(myList);
            result.Sort((first, second) => second.Value.CompareTo(first.Value));
            result.Reverse();

            foreach (KeyValuePair<string, int> pair in result)
            {
                Console.WriteLine(pair.Value);
            }

            Console.ReadLine();
        }
    }
}

The above seems to compile under 3.5 and 2...

Works too - just ran it.

Upvotes: 0

David Morton
David Morton

Reputation: 16505

Have you ensured you're putting this code inside a method? Or do you have it at the class level. Having it at the class level throws 5 errors on build.

Bad

class Program
{
    var myList = new Dictionary<string, int>();
    var result = new List<KeyValuePair<string, int>>(myList);
    result.Sort((x, y) => x.Value.CompareTo(y.Value));  

    static void Main(string[] args)
    {

    }
}

Gives the following errors:

Error 1 Invalid token '(' in class, struct, or interface member declaration
Error 2 Invalid token ',' in class, struct, or interface member declaration
Error 3 Invalid token ')' in class, struct, or interface member declaration
Error 4 Invalid token '(' in class, struct, or interface member declaration
Error 5 Invalid token ')' in class, struct, or interface member declaration

Good

class Program
{
    static void Main(string[] args)
    {
        var myList = new Dictionary<string, int>();
        var result = new List<KeyValuePair<string, int>>(myList);
        result.Sort((x, y) => x.Value.CompareTo(y.Value));  

    }
}

Upvotes: 0

Related Questions