代码之家  ›  专栏  ›  技术社区  ›  Michael

为什么C关键字无法识别[[关闭]

  •  -1
  • Michael  · 技术社区  · 4 年前

    我试着用 outint

    代码如下:

     int.TryParse(part, outint number);
    

    outint does not exist in the current context
    

    应用程序的目标框架是.NET5.0。

    你知道为什么我没被认出来吗?

    4 回复  |  直到 4 年前
        1
  •  2
  •   Will Walsh    4 年前

    我相信你想要的是 out outint . 你需要通过考试 int 关键字后面的变量 外面的 . 结果将出现在这行代码后面的变量中。

        2
  •  2
  •   Chinnaraj    4 年前

    它的 out int outint 在中间留出一个空间,然后再试一次

        3
  •  1
  •   Rahul Shukla    4 年前

    out关键字后面需要空间

    int.TryParse(part, out int number);
    
        4
  •  -1
  •   user555045    4 年前

    outint 发生在 the code sample from our interview with Mads Torgersen on Code Conversations ,但这不是它的工作原理,也不是随附视频中显示的内容。

    documentation of the feature 比这些样品更值得信任。

    out 方法调用的参数列表中的变量,而不是单独的变量声明中的变量。这会产生更紧凑、可读性更强的代码,还可以防止在方法调用之前不经意地为变量赋值。下面的示例与前面的示例类似,只是它在对Int32.TryParse方法的调用中定义了number变量。

    string numberAsString = "1640";
    
    if (Int32.TryParse(numberAsString, out int number))
        Console.WriteLine($"Converted '{numberAsString}' to {number}");
    else
        Console.WriteLine($"Unable to convert '{numberAsString}'");
    // The example displays the following output:
    //       Converted '1640' to 1640
    

    顺便说一下,如果语法真的像 ,如果没有空间,那就真的很奇怪了:也许特征会被限制在一些原始类型和 var 郊游 可能不是功能的工作方式。

    推荐文章