4

Hi I'm not an expert in C# and I found this piece of code and don't really understand what it does.

I never saw the operator => in c# before. Is like a redirect?

public byte[] methodA(byte[] data) => 
  this.methodB(data);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Sinjuice
  • 532
  • 1
  • 4
  • 21

2 Answers2

11

That's called an expression bodied method. It's new in C# 6.0.

It's equivalent to:

public byte[] methodA(byte[] data) {
  return this.methodB(data);
}
Glorin Oakenfoot
  • 2,455
  • 1
  • 17
  • 19
  • 2
    In addition to the answer, here's the link to the [MSDN page](https://msdn.microsoft.com/en-us/magazine/dn802602.aspx). Just scroll to the `Expression Bodied Functions and Properties` section. – Peter Szekeli Mar 16 '16 at 13:58
0

It's a new feature named "Expression Bodied function" in C#6.0 which reduces your lines of code as well. e.g

 //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);
Anwar Ul-haq
  • 1,851
  • 1
  • 16
  • 28