简体   繁体   English

无法使用值 C# 初始化引用变量

[英]Cannot initialize a by-reference variable with a value C#

Having this error Cannot initialize a by-reference variable with a value I'm fairly new to C# just trying trying to fix up a abandoned open source project.出现此错误无法使用值初始化引用变量我对 C# 很陌生,只是试图修复一个废弃的开源项目。

Here's the code:这是代码:

private bool CertificateHandler(bool valueExist)
    {
      if (!CertMaker.rootCertExists() && !CertMaker.createRootCert() || (CertMaker.rootCertIsTrusted() ? 1 : (CertMaker.trustRootCert() ? 1 : 0)) == 0)
        return false;
      // ISSUE: explicit reference operation
      ref string local1 = @this.fiddlerCertInfos._fiddlerCert;
      if (local1 == null)
        local1 = FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.bc.cert", (string) null);
      // ISSUE: explicit reference operation
      ref string local2 = @this._fiddlerCertInfos._privateKey;
      if (local2 == null)
        local2 = FiddlerApplication.Prefs.GetStringPref("fiddler.certmaker.bc.key", (string) null);
      if (!valueExist)
        this.appRegistry.UpdateRegistry(new List<RegistryInfo>()
        {
          new RegistryInfo()
          {
            Name = "FiddlerCert",
            Value = (object) this._fiddlerCertInfos._fiddlerCert,
            RegistryValueKind = RegistryValueKind.String
          },
          new RegistryInfo()
          {
            Name = "PrivateKey",
            Value = (object) this._fiddlerCertInfos._privateKey,
            RegistryValueKind = RegistryValueKind.String
          }
        });
      return true;
    }

Error line:错误行:

ref string local1 = @this.fiddlerCertInfos._fiddlerCert;
ref string local2 = @this._fiddlerCertInfos._privateKey;

Read this:读这个:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#ref-locals https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#ref-locals

You can't assign a local reference variable with a non reference value.您不能为局部引用变量分配非引用值。 So either you need to remove the ref-keyword for the local variable or add 'ref' in front of the value which is assigned.因此,要么您需要删除局部变量的 ref 关键字,要么在分配的值前面添加“ref”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM