【C#】=>符号使用汇总

一、委托

delegate int Method(int a, int b);
Method m
+= (a ,b) => a + b; m(2, 3);

 

二、linq表达式

    internal abstract class GeoService
    {
        internal abstract void Update(GeoLocation location);

        internal abstract string Name { get; }
    }

     private List<GeoService> _geoServices = new List<GeoService>();

     private void NotifyLocationChanged(GeoLocation location)
      {
          // 遍历这个list列表,对每个项执行一下update方法。
          _geoServices.ForEach(g => g.Update(location));
      }

 

三、参数

RelayCommand(() => this.AddPerson(), () => this.CanAddPerson());

() => this.AddPerson() 的意思是一个没有参数的方法,返回 this.AddPerson().

而这个返回值的类型不用指定,系统会自动判断。

 

四、方法

   public override string ToString() => $"{fname} {lname}".Trim();
   public void DisplayName() => Console.WriteLine(ToString());

方法包含单个表达式,它返回的值的类型与方法的返回类型匹配;或者,对于返回 void 的方法,其表达式则执行某些操作。

 

五、只读

public class Location
{
   private string locationName;

   public Location(string name)
   {
      locationName = name;
   }

   public string Name => locationName;//使用表达式主体定义来实现只读属性
}

 

六、属性

public class Location
{
   private string locationName;

   public Location(string name) => Name = name;
//使用表达式主体定义来实现属性 get 和 set 访问器
public string Name { get => locationName; set => locationName = value; } }

 

https://blog.csdn.net/weixin_44231544/article/details/124836588

posted @ 2023-02-20 17:17  不溯流光  阅读(213)  评论(0编辑  收藏  举报