Why am I getting this compilation error in my abstract base class?

橙三吉。 提交于 2019-12-29 01:40:11

问题


I'm trying to extend this plugin for my own use...

https://github.com/jamesmontemagno/Xamarin.Plugins/blob/master/Connectivity/Connectivity/Connectivity.Plugin.Abstractions/BaseConnectivity.cs

But when I copy the code over to Visual Studio, I'm am getting some error.

Do I need to import some special framework in order to use the '=>' operator and 'Invoke' Method? This is an abstract base class. I am using VS2013


回答1:


?. is a feature from C# 6, and as well as the => operator (when used for expression-bodied members), is available in Visual Studio 2015 only.

Read about them here null-conditional operators and here => operator

C# < 6 syntax:

protected virtual void OnConnectivityChanged(ConnectivityChangedEventArgs e)
{
    if (ConnectivityChanged != null)
        ConnectivityChanged.Invoke(this, e);
}



回答2:


?. is Null-conditional operators in C#6. You is using C#6? C#6 featured

Replace this code to

if(ConnectivityChanged != null) 
{ 
    ConnectivityChanged.Invoke(this, e); 
} 

This is equivalent to the our code ConnectivityChanged?.Invoke(this, e);




回答3:


This should do it.

protected virtual void OnConnectivityChanged(ConnectivityChangedEventArg e)
{
   if(ConnectivityChanged != null)
   {
       ConnectivityChanged.Invoke(this,e);
   }
}


来源:https://stackoverflow.com/questions/34161885/why-am-i-getting-this-compilation-error-in-my-abstract-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!