简体   繁体   English

错误 CS0103:当前上下文中不存在名称“x”

[英]error CS0103: The name 'x' does not exist in the current context

I am using linq in a simple console application and I get this error and can't figure out what is wrong.我在一个简单的控制台应用程序中使用 linq,但出现此错误,但无法弄清楚出了什么问题。 Never got this error before with linq.使用 linq 之前从未出现过此错误。

public class Program
{
    public static List<string> names = new List<string>();
    static void Main(string[] args)
    {
        names.Add("Ruben");
        names.Add("Dirk");
        names.Add("Jan");
        names.Add("Klaas");

        var test = names.SingleOrDefault(x => x.EndsWith("k"));
    }
}

在此处输入图片说明

The x variable is only visible within the LINQ expression itself. x变量仅在 LINQ 表达式本身中可见。 So while the debugger is on the assignment statement var test = names.SingleOrDefault... you won't see it.因此,当调试器在赋值语句var test = names.SingleOrDefault...您将看不到它。 You can set a breakpoint on x.EndsWith("k") then you will see the x value when it is hit.您可以在x.EndsWith("k")上设置断点,然后您将看到 x 值。

This is completely normal.这是完全正常的。 It happens when predicate parameter is optimized out when code is compiled and not available to the debugger.它发生在谓词参数在编译代码时被优化并且不可用于调试器时。 When you continue to run the highlighted statement you should see the test variable is evaluated, if there're no other exceptions, which I'm referring to SingleOrDefault() call, it throws an exception when you have multiple results of the predicate test.当您继续运行突出显示的语句时,您应该看到test变量被评估,如果没有其他异常,我指的是SingleOrDefault()调用,当您有多个谓词测试结果时,它会抛出异常。

And also, as @klaus-gütter mentioned the variables in LINQ expressions are visible in the expression execution only, when they're not optimized out.而且,正如@klaus-gütter 提到的,LINQ 表达式中的变量仅在未优化的表达式执行中可见。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误cs0103名称&#39;IEnumerator&#39;在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context 错误CS0103:名称“ DebugDisplayString”在当前上下文中不存在 - error CS0103: The name 'DebugDisplayString' does not exist in the current context 错误CS0103:名称“ ressourcesText”在当前上下文中不存在 - error CS0103: The name “ressourcesText” does not exist in the current context 错误CS0103:名称“ txtReferralCode”在当前上下文中不存在 - error CS0103: The name 'txtReferralCode' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“playfabManager” - error CS0103: The name 'playfabManager' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM