Adding a Command Within C# Get/Set Shorthand

66 Views Asked by At

Here is my understanding of C# get/set shorthand. The long way of defining a property in C#:

private int myVar;

 public int MyProperty
 {
  get { return myVar; }
  set { myVar = value; }
 }

We can shorten it by typing:

public int MyProperty { get; set; }

I have lots of properties defined, and each property always calls a function when it is set. Is there any way we can implement this using the shorthand method? Example of the long way I'm doing:

private int myVar;

 public int MyProperty
 {
  get { return myVar; }
  set { 
     myVar = value;
     //This function gets called every time the setter is called
     MyFunction();
  }
 }

Instead of doing this long method for the bunch of properties I have, is there any way to shorten it? Example [Does not work but I want something that looks like this if possible]:

public int MyProperty {get; set {MyFunction();}; }

Thank you!

0

There are 0 best solutions below