2017年7月23日 星期日

[C#] Auto-implemented Properties History


  • Characteristic
    • 封裝,資料保護及隱藏
  • .NET Framework 1.0
    • Code
    
        public class AutomaticProperties
        {
            private string _Name;
    
            public string getName()
            {
                return _Name;
            }
    
            public void setName(string arg)
            {
                _Name = arg;
            }
    
            private int _Age;
    
            public int getAge()
            {
                return _Age;
            }
    
            public void setAge(int arg)
            {
                _Age = arg;
            }
        }
    
  • .NET Framework 2.0
    • Code snippets (propfull)
    • Code
    
    public class AutomaticProperties
        {
            private string _Name;
    
            public string Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    _Name = value;
                }
            }
    
            private int _Age;
    
            public int Age
            {
                get
                {
                    return _Age;
                }
                set
                {
                    _Age = value;
                }
            }
        }
    
  • .NET Framework 3.0 ~ 5.0
    • Code snippets (prop)
    • Code
    
        public class AutomaticProperties
        {
            public string Name
            {
                get;
                set;
            }
    
            public int Age
            {
                get;
                set;
            }
        }
    
    • Code 進一步縮減
    
        public class AutomaticProperties
        {
            public string Name { get; set; }
    
            public int Age { get; set; }
        }
    
    • Code 唯讀
      • 建構值中初始化
    
        public class AutomaticProperties
        {
            public AutomaticProperties()
            {
                Name = "Lyndon";
                Age = 30;
            }
    
            public string Name { get; private set; }
    
            public int Age { get; private set; }
        }
    
    • Code 唯讀,進一步縮減
      • 因不是標記成 abstract 或 extern,因此必須宣告主體
    
        public class AutomaticProperties
        {
            /// 
            /// 直接回傳值
            /// 
            public string Name
            {
                get
                {
                    return "Lyndon";
                }
            }
    
            public int Age
            {
                get
                {
                    return 30;
                }
            }
        }
    
  • .NET Framework 6.0
    • Code
    
        public class AutomaticProperties
        {
            public string Name { get; } = "Lyndon";  
    
            public int Age { get; set; } = 30;
        }
  • Ref:
    • http://blog.xuite.net/sunnysoap/r/65597736
    • http://blog.csdn.net/heqi915/article/details/1589777
    • http://alansong.pixnet.net/blog/post/55999534-c%23-get-set-%E5%8F%8A%E8%87%AA%E5%8B%95%E5%B1%AC%E6%80%A7
    • https://docs.microsoft.com/zh-tw/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

沒有留言:

張貼留言