c# - 如何修复错误 CS1738 命名参数规范必须在指定所有固定参数后出现

标签 c#

public static Mutex CreateMutex(){
    MutexAccessRule rule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
    MutexSecurity mutexSecurity = new MutexSecurity();
    mutexSecurity.AddAccessRule(rule);
    bool createdNew;
    return new Mutex(initiallyOwned: false,"Global\\E475CED9-78C4-44FC-A2A2-45E515A2436", out createdNew,mutexSecurity);
}

Error CS1738 Named argument specifications must appear after all fixed arguments have been specified

最佳答案

所以引用 C# Doc

Named arguments, when used with positional arguments, are valid as long

  • as they're not followed by any positional arguments


所以这就是原因,为什么你会遇到编译错误。

使用 C# 7.2 及更高版本时,它说:

Named arguments, when used with positional arguments, are valid as long

  • as they're used in the correct position.


有关更多信息,请参阅:
Named and Optional Arguments

所以如果我们看一下 constructor :
public Mutex (bool initiallyOwned,
              string name,
              out bool createdNew,
              System.Security.AccessControl.MutexSecurity mutexSecurity);

我们将看到在您的情况下,位置的顺序是正确的。
如果您要切换到 C# 7.2 或更高版本,您的代码将被编译。

但是对于较低版本,您有两种选择:
  • 删除 initiallyOwned 的参数命名
  • 对所有参数使用命名参数

  • 喜欢:
    return new Mutex(initiallyOwned: false,
                     name: "Global\E475CED9-78C4-44FC-A2A2-45E515A2436",
                     createdNew: out createdNew,
                     mutexSecurity: mutexSecurity); 
    

    使用第二个选项时,命名参数的位置不再重要。

    关于c# - 如何修复错误 CS1738 命名参数规范必须在指定所有固定参数后出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57062106/

    相关文章:

    c# - .NET 的替代方案提供了关于 uris 和 url 的 api

    c# - CaSTLe DynamicProxy 拦截器在使用不同程序集时出现问题

    c# - 在 select 语句中调用 Helperfunction,保留多个值的返回结果

    c# - 如何一般地格式化 OracleNumber

    javascript - Webbrowser 控件如何更改窗口 javascript 属性?

    c# - DDay.ICal 与谷歌日历返回多次出现

    c# - 如何从另一个类的 listBox1_SelectedIndexChanged 事件中获取值?

    c# - 设置复选框的高度不起作用

    c# - 欺骗 JS 对象

    c# - 无法使用 DeflateStream 与 C# 一起膨胀