为什么这个 Lambda 函数在 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));
时,它在第 3 行抛出 5 个错误
在构建ASP.NET 2.0 的屏幕截图 替代文本 http://img696.imageshack.us/img696/3668/lambdas.jpg< /a>
这是来自 .NET 2.0 中的控制台应用程序 替代文本 http://img138.imageshack.us/img138/4788/lambda2.jpg< /a>
那么你认为哪里出了问题?
给约翰 替代文本 http://img705.imageshack.us/img705/3618/lambda3.jpg< /a>
好的..所以这是一个 RAR 文件,其中包含一个控制台应用程序和一个为 .NET 2.0 编写的 Web 应用程序 Lambda.rar
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是为了尝试将这一点集中到一个地方:
C# 编程语言在很大程度上独立于 .NET Framework。其中一个例子是 Visual Studio 2008 引入了 C# 编程语言的第 3 版,该语言支持 lambda 表达式。同一版本的 Visual Studio 2008 还引入了 .NET Framework 3.5 版。它还引入了针对框架 2.0、3.0 或 3.5 版本的功能,同时允许您使用该语言的 2.0 或 3.0 版本。
例如,这允许您在面向 .NET Framework 版本 2.0 或 3.0 的程序中使用 C# 3.0 功能。
不知何故,您的 ASP.NET 应用程序(或者是网站)被设置为使用 C# 编程语言 2.0 版本。您的控制台应用程序设置为使用版本 3.0。这就是它在控制台应用程序中工作而不是在 ASP.NET 应用程序中工作的原因。
ASP.NET 始终支持与控制台应用程序相同的 .NET Framework 和 C# 编程语言功能。如果您发现两者之间存在差异,那么这是您的设置的差异,而不是平台的差异。这是基于我自 1.0 版本 Beta 以来对 ASP.NET 的了解。
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.
您的第二个屏幕截图从文件顶部(使用指令)裁剪下来,并且您明确提到“.NET 2”,无论是偶然还是故意的。那么,显而易见的问题是:
您使用 C# 3 吗?因为 Lambda 在 C#2 中不可用。 (注意:重要的是语言版本,而不是 /NET 框架版本!)
多重编辑:该死,我的第一次编辑让我更加错误:-)
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 :-)
您是否确保将此代码放入方法中?或者你在课堂上有它吗?在类级别使用它会在构建时引发 5 个错误。
Bad
给出以下错误:
好
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
Gives the following errors:
Good
上面的代码似乎可以在 3.5 和 2 下编译...
也可以工作 - 只是运行它。
The above seems to compile under 3.5 and 2...
Works too - just ran it.