-1

I'm unclear as to how the => operator in C# works and would appreciate some advice.

I've seen it used after a get and set.

Can someone tell me, can I use that operator in this code:

public static readonly BindableProperty ParamProperty =
            BindableProperty.Create(
                nameof(Param),
                typeof(string),
                typeof(GridTemplate),
                default(string));

public string Param
{
   get { return (string)GetValue(ParamProperty); }
   set { SetValue(ParamProperty, value); }
}
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • Possible duplicate of [Expression bodied get / set accessors feature in c# 7.0](https://stackoverflow.com/questions/43681882/expression-bodied-get-set-accessors-feature-in-c-sharp-7-0) – mjwills Jul 30 '18 at 02:28

2 Answers2

3
public string Param
{
    get => (string) GetValue(ParamProperty);
    set => SetValue(ParamProperty, value);
}

This is C# 7 syntax.

fghzxm
  • 1,185
  • 7
  • 19
3

Basically Expression bodied members are just a short-hand way to achieve regular tasks. They "can" make your code look cleaner, and save printable characters. Though, there is nothing really special about them other than the syntactic sugar which denote them.

You can read more about them here. Additionaly, i have summarised and condensed the info and added some pepper and salt to taste

Expression-bodied members (C# programming guide)

Expression body definitions let you provide a member's implementation in a very concise, readable form. You can use an expression body definition whenever the logic for any supported member, such as a method or property, consists of a single expression.

Methods

An expression-bodied method consists of a single expression that returns a value whose type matches the method's return type, or, for methods that return void, that performs some operation.

public void DisplayName() => Console.WriteLine(ToString());

// equivalent 

public void DisplayName()
{
    Console.WriteLine(ToString())
}

Constructors

An expression body definition for a constructor typically consists of a single assignment expression or a method call that handles the constructor's arguments or initializes instance state.

public class Location
{
   private string locationName;

   public Location(string name) => Name = name;

   // equivalent 

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

Finalizers

An expression body definition for a finalizer typically contains cleanup statements, such as statements that release unmanaged resources

public class Destroyer
{
   public override string ToString() => GetType().Name;

   ~Destroyer() => Console.WriteLine($"The {ToString()} destructor is executing.");

   // equivalent 

   ~Destroyer()
   {
       Console.WriteLine($"The {ToString()} destructor is executing.");
   }
}

Property get and set statements

If you choose to implement a property get accessor yourself, you can use an expression body definition for single expressions that simply set or return the property value.

public string Name
{
   get => locationName;
   set => locationName = value;
} 

// equivalent 

public string Name
{
   get 
   {
       return locationName;
   }
   set
   {
      locationName = value;
   }
} 

Read-only properties

PropertyName => returnValue;

// equivalent 

public string Name
{
   get 
   {
       return someValue;
   }
}

Indexers

Like properties, an indexer's get and set accessors consist of expression body definitions if the get accessor consists of a single statement that returns a value or the set accessor performs a simple assignment.

public string this[int i]
{
   get => types[i];
   set => types[i] = value;
}

// equivalent 

public string this[int i]
{
   get
   {
       return types[i];
   }
   set
   {
       types[i] = value;
   }
}

Also not to be confused with Auto-Implemented initialized Properties

C# Auto-Implemented initialized Properties

public string Name { get; set; } = "Joe";

Which is basically just like setting the property in your constructor

TheGeneral
  • 79,002
  • 9
  • 103
  • 141