Reputation: 61
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
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
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
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
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
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
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