0
        private string _destSystem;
        /// <summary>
        /// target system
        /// </summary>
        public string DestSystem
        {
            get { return _destSystem; }
            set { _destSystem = value; RaisePropertyChanged(() => DestSystem); }
        }

This field "_destSystem" is completely redundant, How can I remove this field without affecting the function?

Ryu
  • 1
  • 1
  • You can't. If calling code sets `DestSystem` and later on reads `DestSystem`, without a field the semantics will change. Something has to store the value somewhere. (You could remove the field if nothing ever reads the property, but even then you're going to change the semantics because presumably you wouldn't want to raise `PropertyChanged` if the property is being set to a value that it's already set to.) – Matthew Watson Sep 08 '21 at 08:07
  • public string DestSystem{ get; set;} works because the compiler generates the field for you behind the scenes., if you need additional logic for the property, you need to maintain the field yourself. – Schwarzie2478 Sep 08 '21 at 08:10
  • Yes, I just hope that there is a way to keep the PropertyNotification while still allowing the compiler to automatically generate this field. Well, Thank you @Matthew Watson, it seems that it is not possible at present, and I hope that Microsoft can make improvements in the future. – Ryu Sep 08 '21 at 08:23
  • Yes, I just hope that there is a way to keep the PropertyNotification while still allowing the compiler to automatically generate this field. Well, Thank you @Schwarzie2478 , it seems that it is not possible at present, and I hope that Microsoft can make improvements in the future. – Ryu Sep 08 '21 at 08:23
  • You can always look at the linked answer, there you have the solution for adding this kind of code behind the scenes ( code-weaving or post processing) interesting topic but a bit advanced, can be tricky to debug, you have been warned :) – Schwarzie2478 Sep 08 '21 at 08:25
  • Yes, Postsharp can be implemented in this way through static weaving or some proxy mode. I even tried to write some simple demos with it, but this is a bit overkill for my current project, haha – Ryu Sep 08 '21 at 08:45
  • There's a proposed feature using a `field` keyword which would address this issue, but note that it hasn't (yet) been included in the C# 10 milestones, so it may never happen. See https://exceptionnotfound.net/bite-size-csharp-10-semi-auto-properties-using-field-keyword/ Here's the proposal: https://github.com/dotnet/csharplang/issues/140 – Matthew Watson Sep 08 '21 at 09:14
  • Look what I found? C#10 is already supported, haha! Microsoft is awesome https://dev.to/dotnetsafer/c-10-roadmap-exposing-new-features-2ifg – Ryu Sep 09 '21 at 06:30

0 Answers0