I suggest you ...

Allow custom auto-property implementations

This would allow us to write a form of extension method for auto property getters and setters. The idea behind this is that we would be able to encapsulate commonly repeated setter logic into a separate unit to make our code DRY. These methods should use generic typing to set the type of the property and the type of the class that this method could be applied in. These methods should also provide references to the backing field, the new value (if it is a setter), and the containing class (to provide class level access in the method)

I.E. - Create a hybrid of an extension method available to a property:
//Setter has three arguments (new value, backing field, and containing class) and returns void
public void CantBeNull<TProperty, TClass>(this TProperty value, TProperty backingField, TClass containingClass) where TProperty : class where TClass : IMyInterface
{
if (value != null) backingField = value;
}

//Setter has three arguments (new value, backing field, and containing class) and returns void
public void MustBePositive<TProperty, TClass>(this int value, TProperty backingField, TClass containingClass) where TProperty : int
{
if (value != null && value >= 0) backingField = value;
}

//Getter has two arguments (backing field and containing class) and returns the property type
public TProperty ReturnDefaultIfNull<TProperty, TClass>(TProperty backingField, TClass containingClass) where TProperty : class, new()
{
return backingField ?? new T();
}

Then in usage:

public int X { get; MustBePositive set; } = x;
public String Y { get; CantBeNull set; } = y;
public Object Z { ReturnDefaultIfNull get; CantBeNull set; } = z;

The idea being that you can use a well defined getter or setter independently of one another. You would provide the method implementation with type constraints in order to allow users to interact with the class from the auto property implementation. In the MVVM world with ReactiveUI this would allow you to cleanup a million entries of this:

public class MyViewModel : ReactiveObject
{
private Boolean _showSomething;
private String _statusText;

public Boolean ShowSomething
{
get { return _showSomething; }
set { this.RaiseAndSetIfChanged(ref _showSomething, value); }
}

public Boolean StatusText
{
get { return _statusText; }
set { this.RaiseAndSetIfChanged(ref _statusText, value); }
}
}

with this:

public TProperty ReactiveSet<TProperty, TClass>(this TProperty value, TProperty backingField, TClass containingClass) where TProperty : class where TClass : IReactiveObject
{
containingClass.RaiseAndSetIfChanged(ref backingField, value);
}

public class MyViewModel : ReactiveObject
{
public Boolean ShowSomething { get; ReactiveSet set; }
public Boolean StatusText { get; ReactiveSet set; }
}

This would be majorly awesome to see in C# - It can get really annoying really quickly when you have a large enterprise-level application that has thousands of backing fields simply to support one liner pieces of validation and such...the above code can replace a 6 line property with 1 line and a defining method

8 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Robert PetzRobert Petz shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    2 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Christopher HawsChristopher Haws commented  ·   ·  Flag as inappropriate

        I am looking for exactly what Julien mentioned. Having the ability to implement very basic logic into the property without it needing to have a private variable in the class would be very helpful.

      • Julien NJulien N commented  ·   ·  Flag as inappropriate

        I was looking for a feature like that (a way to add some code without having to create a backing field) but maybe simpler, using keywords like the "value" used in the setter.

        Something like (obviously with better keywords) :
        public string MyProperty
        {
        get {return currentvalue ?? String.Empty; }
        set {if(value != null) currentvalue = value; }
        }

        But your solution is much better and would allow really nice features !

      Feedback and Knowledge Base