【问题标题】:Is it possible to use a variable as a default parameter in C#?是否可以在 C# 中使用变量作为默认参数?
【发布时间】:2015-11-12 04:49:13
【问题描述】:

我正在使用"BrowserSession" 并添加了 2 个变量来跟踪用户名和密码,以登录网站。

    private string _username;
    private string _password;

    public StoredAccountInfo RefreshAccountData(string username = _username, string password = _password)
    {
        if ((string.IsNullOrEmpty(username)) || (string.IsNullOrEmpty(password))
        {
            //throw Exception("Password Or Username Not Set");
        }
        BrowserLogin(username, password);//In case user is not logged in
        StoredAccountInfo retdata = new StoredAccountInfo();
        //populate retdata
        return retdata;

我还添加了设置 _username 和 _password 变量的函数

    public void BrowserLogin(string username, string password)
    {
        if (Cookies == null)
        {
            //Do Site Login Here If not Logged In
            Get(constants.BaseUrl);
            FormElements["UserName"] = username;
            FormElements["PassWord"] = password;
            Post(constants.loginUrl);
            _username = username;
            _password = password;
        }
    }

然而

public StoredAccountInfo RefreshAccountData(string username = _username, string password = _password)

给我语法错误 "default parameter value for 'username' must be a compile-time constant"

有人知道解决方法或解决方案吗?我不想每次调用 RefreshAccountData 方法时都传递用户名和密码,但是如果在第一次浏览器登录之前调用它,我希望能够从中设置用户名和密码变量。

我可能只是完全错过了一种非常简单的方法来做到这一点。但我似乎想不出来。

【问题讨论】:

  • 重载方法怎么样?不要再试图让可选参数做他们不应该做的事情。
  • 我不确定重载是如何工作的。虽然我会调查一下,但奇怪的是,我发现它们相当混乱。但是,有很多答案可以满足我的需要。
  • @JasonBrown 我从来没有见过有人学会了可选参数但不会重载......你应该先学习重载。
  • @Sweeper 通过反复试验学习自己,我一直在跳过一堆语言(java/c++/c#/etc)。但是我在了解重载的工作原理之前就发现了可选参数。
  • 感谢所有快速答复。我终于应该研究一下超载了,我猜我已经推迟了太久了。

标签: c# optional-parameters


【解决方案1】:

如果传递了 null,您可以默认为 null 并使用这些变量:

public StoredAccountInfo RefreshAccountData(string username = null, string password = null)
{
    username = username ?? _username;
    password = password ?? _password;
    if ((string.IsNullOrEmpty(username)) || 
        (string.IsNullOrEmpty(password))
    {
        //throw Exception("Password Or Username Not Set");
    }

或者你可以提供第二个不接受这些参数的方法并将变量传递给你的主方法,这会让你捕捉到调用者实际上试图传递一个碰巧为空的用户名或密码并抛出一个错误。

public StoredAccountInfo RefreshAccountData()
{
    RefreshAccountData(_username, _password);
}

【讨论】:

    【解决方案2】:

    重载方法:

    public void BrowserLogin(string username, string password)
    {
    ...
    }
    
    public void BrowserLogin(string username)
    {
      BrowserLogin(username, _password);
    }
    
    public void BrowserLogin()
    {
      BrowserLogin(_username, _password);
    }
    

    参数对象:

    public class LoginParams 
    {
      public string UserName { get; set; }
      public string Password { get; set; }
    }
    
    public void BrowserLogin(LoginParams aParams)
    {
      if (aParams.UserName == null)
        <use this._username>
      if (aParams.Password == null)
        <use this._password>
    ...
    }
    
    BrowserLogin(new LoginParams());
    BrowserLogin(new LoginParams() { UserName = username });
    BrowserLogin(new LoginParams() { UserName = username, Password = password });
    

    【讨论】:

    • 我对此只有一个问题,不是专门针对这种情况,而是更一般地说,是否可以仅使用重载设置密码(因此可以选择仅设置用户名或仅设置密码?)使用参数名称?
    • 不幸的是 - 没有。您可以使用“javascript”样式进行操作 - 请参阅更新。
    【解决方案3】:

    您不能将变量用作可选参数。您必须声明该方法的重载。好难过! :(

    public StoredAccountInfo RefreshAccountData() {
        return RefreshAccountData (_username, _password);
    }
    

    不幸的是,没有办法为此使用可选参数。

    让我解释一下为什么你不能。可选参数值被放入编译的代码中。所以如果你改变变量的值,编译的代码不会改变,对吧?长话短说,编译后的代码无法检测到变量的值发生了变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-16
      • 2019-11-10
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 2020-01-27
      • 2016-07-03
      • 2021-07-05
      相关资源
      最近更新 更多