我在這裏丟失了什麼。C#7使用模式匹配不起作用
的Visual Studio告訴我使用內聯模式匹配,並重新編寫代碼爲我,但是當它出現錯誤:
Severity Code Description Project File Line Suppression State Error CS8121 An expression of type TReturnState cannot be handled by a pattern of type LightState. DataModels C:\Users\Michael\Documents\windows\GCMS UWP\Models\Models\Elements\Lights\Light.cs 77 Active
這裏是原代碼:
public override void UpdateState<TReturnState>(TReturnState returnState)
{
var newState = returnState as LightState;
if (newState != null)
State = newState;
base.UpdateState(returnState);
}
這就是當VS爲我重做它時的樣子。
public override void UpdateState<TReturnState>(TReturnState returnState)
{
if (returnState is LightState newState)
State = newState;
base.UpdateState(returnState);
}
我更喜歡調整的方式,但我得到的錯誤。我錯過了什麼或者這是一個錯誤?
包我使用:
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.3.1",
"Newtonsoft.Json": "9.0.1",
"System.ValueTuple": "4.3.0"
},
'State =(LightState)newState;' – Hogan
@SirRufo它怎麼沒用它更新一個叫做「State」的全局變量? – Hogan
@Hogan哎呀,你是對的...... o) –