When using the C # property, if the recipient or part of it processes the underlying data (if necessary)?

In the code base that I supported, I found this exact class inserted below. The property logPath getsdoes some work. I think it’s better to do the job in set- this will be done only once. However, partly because this is how the class is written, partly because this property is converted to xml, and partly because I am afraid that I might miss something in the debugger, I have doubts.

Also, if the element never existed in xml, and this turned out to be optional, I think I will get zero for the value. I might want to distinguish between the absence of an element and getting an empty value. I suggest that I may have a private member boolthat can help me discover this - this will be an argument for working in set, not get. Thus, code optimizers work these days, so performance is rarely a real problem. It is more likely to "figure it out and not think about it later." This is just one example, and some massaging properties often do.

Could you say that it is always better to work in set? In get? It depends? Mixed style won't bother you with one bit as long as it works?

Thank.

namespace MyNamespace
{
    using System;
    using System.Xml.Serialization;

    /// <summary>
    /// The LoggingListener class encapsulates the "logListener" 
    ///      element of config file, and puts the "logPath" 
    ///      attribute in a file path string.
    /// </summary>
    public class LoggingListener
    {
        private string logPathValue;

        /// <summary>
        /// Gets or sets the LOCAL file path to a log file 
        /// which will be written during operation of the Updater.
        /// </summary>
        [XmlAttribute("logPath")]
        public string LogPath
        {
            get
            {
                return this.logPathValue == null ? 
                         String.Empty : this.logPathValue;
            }

            set
            {
                this.logPathValue = value;
            }
        }
    }
}

: ... , .

+3
4

getters seters :

  • - "B", "A"
  • , "A" , "A" , "A" ,
  • "A" , ,
  • null

:

public class LoggingListener
{
    private string logPathValue = String.Empty;

    [XmlAttribute("logPath")]
    public string LogPath
    {
        get { return logPathValue; }
        set
        {
            if(value == null) throw new ArgumentNullException();
            this.logPathValue = value;
        }
    }
}

, , "XmlAttribute" XmlSerializer. DefaultValueAttribute" ComponentModel XSD / . , :

public class LoggingListener : ILogListenerSettings
{
    private string logPathValue;

    [XmlAttribute("logPath")]
    public string LogPath
    {
        get { return logPathValue; }
        set { logPathValue = value; }
    }

    string ILogListenerSettings.FullLogPath
    {
        get
        {
            string path = logPathValue;
            if(String.IsNullOrEmpty(path))
                path = Environment.CurrentDirectory;
            path = Path.GetFullPath(path);
            Directory.Create(path);
            return path;
        }
    }
}
+2

. . , , NullReferenceException, LogPath LoggingListener - , , null get ( , ).

, , , , set, , LogPath null, null , , ( ).

: , - :

public class LoggingListener {
    private readonly string _logPath;

    public LoggingListener(string logPath) {
        _logPath = logPath ?? string.Empty;
    }

    public string LogPath {
        get { return _logPath; }
    }
}

, , . , , , , , , .

+4

, get set accessor. :

var value = null;
var listener = new LoggingListener();
listener.LogPath = value;
if(listener.LogPath != value)
{
    // how could we get here?
}

, null . , - get/set, , null .

null, set accessor.

+3

the only way to make sure logPathValue is initialized is to do it yourself ...

public class LoggingListener
{
    private string logPathValue = string.Empty;
0
source

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


All Articles