C# 中的未知运算符

  • 本文关键字:运算符 未知 c#
  • 更新时间 :
  • 英文 :

嗨,我

不是 C# 方面的专家,我找到了这段代码,但并不真正了解它的作用。

我以前从未见过运算符=> 在 c# 中。像重定向吗?

public byte[] methodA(byte[] data) => 
  this.methodB(data);

这称为表达式主体方法。它是 C# 6.0 中的新增功能。

它相当于:

public byte[] methodA(byte[] data) {
  return this.methodB(data);
}

这是 C#6.0 中一个名为"Expression Body 函数"的新功能,它也减少了代码行数。例如

 //Old way
        public string Name
        {
            get
            {
                return "David";
            }
        }
//New way
        public string Name => "David";
//old way 
        public Address GetAddressByCustomerId(int customerId)
        {
            return AddressRepository.GetAddressByCustomerId(customerId);
        }
//New Way
        public Address GetAddressByCustomerId(int customerId) => 
               AddressRepository.GetAddressByCustomerId(customerId);

最新更新