![](/img/trans.png)
[英]Is passing a struct value to a method by-reference in C# an acceptable optimization?
[英]Cannot initialize a by-reference variable with a value C#
出現此錯誤無法使用值初始化引用變量我對 C# 很陌生,只是試圖修復一個廢棄的開源項目。
這是代碼:
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;
}
錯誤行:
ref string local1 = @this.fiddlerCertInfos._fiddlerCert;
ref string local2 = @this._fiddlerCertInfos._privateKey;
讀這個:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#ref-locals
您不能為局部引用變量分配非引用值。 因此,要么您需要刪除局部變量的 ref 關鍵字,要么在分配的值前面添加“ref”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.