Are proxy properties a good style?

I have a class with a string property in which there are actually several strings connected to a separator.

I am wondering if there is a good form for a proxy property, for example:

public string ActualProperty
{
    get { return actualProperty; }
    set { actualProperty = value; }
}

public string[] IndividualStrings
{
    get { return ActualProperty.Split(.....); }
    set 
    { 
            // join strings from array in propval .... ;
            ActualProperty = propval;
    }
}

Are there any risks that I have missed?

+3
source share
6 answers

Binding two settable properties together is, in my opinion, bad juju. Switch to using explicit get / set methods instead of properties if this is really what you want. Code that has unobvious side effects will almost always bite you later.

Keep things simple and straightforward.

, , , , , , struct/class , .

+2

, - , - . , , , , .

, ;

  • GetJoinedString(string seperator).
  • SetStrings(string joined, string seperator) Parse(string joined, string seperator).

, , . , , , CSV , . . , .

+2

"". ( , (), Split(), ), IndividualStrings , ActualProperty, ActualProperty , . , , ... , , , .

+1

; .

, , , , , , .

"" , . ( ), , , . , , .

; . . , , .

, :

, . , . , , .

:

YourClass i = new YourClass();
i.IndividualStrings[0] = "Hello temporary array!";

IndividualStrings, , .

public string ActualProperty { get; set; }

public string[] GetIndividualStrings()
{
    return ActualProperty.Split(.....);
}

public void SetFromIndividualStrings(string[] values)
{
    // join strings from array .... ;
}
+1

, , "" - , , - , , , . , - .

, , .

0

, . , .

, IndividualStrings : string [] SplitActualProperty() void MergeToActualProperty ( []).

0

Source: https://habr.com/ru/post/1697074/


All Articles