42

You know how you can have a property that automatically generates a backing field? Like if I go:

public String SomeProperty {get; set;}

I know that if I want to add code to that property I have to create the backing field as so:

 public string someProperty = string.Empty;
 public string SomeProperty
 {
     get { return someProperty; }
     set
     {
         someProperty = value;
         DoSomething();
     }
 }

Basically, what I want to know is... is there any way to do this but without having to create the backing field? For example I could use it to trigger some kind of event that occurs when a property is set. I'm looking for something like this:

 public string SomeProperty
 {
     get;
     set { this.OnSomeEvent; }
 }

But I know that'll cause a compile error because get needs do declare a body if set does.

I've researched and I cant find anything, but I thought I'd check to see if anyone knew.

I guess what I'm really after is some way to trigger an event when a property is changed but without having to add all that extra clutter. Any suggestions?

ashley
  • 521
  • 1
  • 5
  • 6
  • 9
    Type 'propfull' followed by tab tab - a quick way to make a property with instance field. – andrewb Jul 24 '13 at 04:12
  • Good question. If it was like javascript, "this" could refer to the property itself hence the backing field. – toddmo Jan 05 '17 at 19:07
  • You might also expose the doSomething itself as a method in the class. In cases where you dont need a return value – Peter Apr 09 '18 at 11:12
  • @toddmo, you would lose the context of the owning class. – Nathan Goings Sep 18 '20 at 16:11
  • The `field` keyword might be added to C#, see https://github.com/dotnet/csharplang/issues/140, which removes "when no additional logic" requirement for auto properties. It didn't make it into C# 10 nor 11, but latest comment from compiler team says C# version 12 might have it. – yzorg Oct 28 '22 at 16:02

3 Answers3

27

Simple answer is no, you can't have it both ways. From .NET Docs:

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55
Claies
  • 22,124
  • 4
  • 53
  • 77
2

There are not any solutions for this built into the framework, and you cannot modify existing types via reflection (in order to add the logic at runtime). The only way to accomplish this seems to be at compile time.

There is a product http://www.postsharp.net/ that can accomplish this (intercept property/method calls), and there does appear to be a free edition.

Maximum Cookie
  • 407
  • 3
  • 6
  • postsharp is a great alternative, which allows you to do aspect oriented programming (AOP). AOP isn't as much about reducing clutter as it is about managing Cross Cutting Concerns (behaviors that are used across a large number of business requirements, and cannot easily be isolated) – Claies Jul 24 '13 at 04:50
2

The field keyword might be added to C#, see https://github.com/dotnet/csharplang/issues/140, which removes "when no additional logic" requirement for auto properties.

It didn't make it into C# 10 nor 11, but latest comment from compiler team says C# version 12 might have it. They release yearly, so that would be Nov 2023.

yzorg
  • 4,224
  • 3
  • 39
  • 57
  • 1
    examples: https://exceptionnotfound.net/bite-size-csharp-10-semi-auto-properties-using-field-keyword/ – yzorg Oct 28 '22 at 16:05