在之前的开发过程中,由于没有遇到过问题,所以就没有发觉这两者的区别,今天在这里做一个记录:

比如:

字段:

public class Student
{

  public string Name;//字段

  public int Age;//字段

}

  

属性

public class Student
{

  public string Name{get;set;}//属性

  public int Age{get;set;}//属性

}

 1.两者的相同点:

   都可以被new对象时,传递值

2.两者的不同点

  当类被继承是,字段值无法被继承,属性值可以被继承

3.两者的注意点

  a.字段最好 private

  b.属性最好 public

  c.{get;set;}可读可写

    {get;}只读不写

    {set;}???

相关文章:

  • 2022-12-23
  • 2021-10-16
  • 2022-12-23
  • 2021-06-23
  • 2021-07-23
猜你喜欢
  • 2021-08-27
  • 2021-09-13
  • 2021-07-09
  • 2021-07-21
相关资源
相似解决方案