anyaeats160carrots
anyaeats160carrots

Reputation: 61

c# modify getter/setter but keep the short form

I need to do a minor check in C# setter - check if property is an empty string. Right now I ended up with structure like that:

        private string property;
        public string Property
        {
           get
           {
              return property;
           }
           set
           {
              if (value.IsNotEmpty())
              {
                   property = value;
              }
           }
        }

Instead of

public string Property { get; set; }

6 lines instead of 1. Is there a way to insert the logic, but keep it condensed and elegant?

Upvotes: 6

Views: 6688

Answers (6)

Tod
Tod

Reputation: 2524

Based on the answer from @BradleyDotNET I created the following struct to hold NonEmptyStrings, basically if set empty it reverts to null.

[JsonConverter(typeof(NonEmptyStringSystemTextJsonConverter))]
public readonly struct NonEmptyString
{
    [AdaptMember] //Mapster attribute to allow for cloning
    private readonly string? _value;

    
    public NonEmptyString(string? value) => _value = string.IsNullOrWhiteSpace(value) ? null : value;

    public static implicit operator NonEmptyString(string? value)
    {
        return new NonEmptyString(value);
    }

    public static implicit operator string?(NonEmptyString value)
    {
        return value._value;
    }

    public override string ToString() => _value!;
    private class NonEmptyStringSystemTextJsonConverter : JsonConverter<NonEmptyString>
    {
        public override NonEmptyString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new(reader.GetString());

        public override void Write(Utf8JsonWriter writer, NonEmptyString value, JsonSerializerOptions options) => writer.WriteStringValue(value._value);
    }
}

Upvotes: 1

Andii
Andii

Reputation: 337

As of C# 7, properties support arrow syntax, making the following possible:

private string property;
public string Property
{
    get => property;
    set => property = value.IsNotEmpty() ? value : property;
}

Upvotes: 3

Esteban Elverdin
Esteban Elverdin

Reputation: 3582

It's not exactly what you ask, but perhaps you can use DataAnnotations, for not allowing an empty string. Something like this, in this case a validation exception is raised if the property is null, an empty string (""), or contains only white-space characters.

  [Required]
  public string Property { get; set; }

Upvotes: 2

deathismyfriend
deathismyfriend

Reputation: 2192

You can always make it like this.

It does compact it but offers no performance boost doing it this way.

    private string property;
    public string Property { get { return property; } set { if (value.IsNotEmpty()) property = value; } }

Upvotes: 1

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

No, there is no syntax sugar for such cases (at least up to C# 5.0 - current for 2014).

You can format them differently and use ?: instead of if if it looks nice enough to you:

    public string Property
    {
       get { return property; }
       set { property = value.IsNotEmpty() ? value: property;}
    }

Upvotes: 5

BradleyDotNET
BradleyDotNET

Reputation: 61339

No

Auto-properties (or the "short form") can have access modifiers, but no logic. You are stuck with the code you have.

One thing you could do is to encapsulate your string in an object that allows for an implicit cast from string (and to string), and checks IsNotEmpty before assigning to a underlying value. Also not the most elegant solution, but it would probably allow you to keep the syntactic sugar.

Upvotes: 7

Related Questions