zoukankan      html  css  js  c++  java
  • C# 3.0 / C# 3.5 自动属性

    自动属性的好处

      自动属性简化了我们在做 C# 开发的时候手写一堆私有成员 + 属性的编程方式,我们只需要使用如下方式声明一个属性,编译器就会自动生成所需的成员变量。

    传统属性概念

      属性的目的一是封装字段,二是控制读写权限及字段的访问规则(如年龄、生日范围),平时主要是用来封装读写权限

      在 C# 3.0 之前,我们是这样来实现属性的:

        private int id;
        public int Id
        {
          get{ return id; }
          set{ id = value; }
        }

      使用自动属性是这样实现的:

        public class User
        {
          public int Id { get; set; }
          public string Name { get; set; }
          public int Age { get; set; }
          public Address Address { get; set; }
        }

      使用自动属性的话,程序员写的代码少了,机器做的事情就多了,那我们到底要不要使用它呢?

      如果是针对读写权限的封装,就推荐使用,因为它是在编译的时候产生了负担,并不是在运行的时候,所以不会影响客户运行程序时的效率!

  • 相关阅读:
    Leetcode Binary Tree Paths
    Leetcode Lowest Common Ancestor of a Binary Tree
    Leetcode Lowest Common Ancestor of a Binary Search Tree
    Leetcode Path Sum
    Leetcode Symmetric Tree
    Leetcode Invert Binary Tree
    Leetcode Same Tree
    Leetcode Maximum Depth of Binary Tree
    Python Json&Pickle&模块
    Python Shelve模块
  • 原文地址:https://www.cnblogs.com/zhangchaoran/p/8679037.html
Copyright © 2011-2022 走看看