
字段1.什么是字段字段本质是变量变量的作用是存储数据字段是为一个对象/类型存储数据多个字段组合起来表达一个对象/类型当前的状态。实例字段表示实例对象当前的状态隶属于实例静态字段表示类型当前的状态隶属于类型static修饰有 static → 属于类没 static → 属于实例这个说法只对成员字段、方法、属性成立。class Program { static void Main(string[] args) { ListStudent stuList new ListStudent(); for(int i0;i100;i) { Student stunew Student(); stu.Age22; stu.Scorei; stuList.Add(stu); } int totalAge0; int totalScore0; foreach(var stu in stuList) { totalAgestu.Age; totalScorestu.Score; } Student.AverageAgetotalAge/Student.Amount; Student.AverageScoretotalScore/Student.Amount; Student.ReportAmount(); Student.ReportAverageAge(); Student.ReportAverageScore(); } } class Student { //实例字段 public int Age; public int Score; //静态字段(static修饰) public static int AverageAge; public static int AverageScore; public static int Amount; public Student() { Student.Amount; //每个Student实例创建时总数1 } public static void ReportAverageAge() { Console.WriteLine(Student.AverageAge); } public static void ReportAverageScore() { Console.WriteLine(Student.AverageScore); } public static void ReportAmount() { Console.WriteLine(Student.Amount); } }2.字段的声明*字段是类型的成员所以要在类体里声明不要写到函数体里那就成了局部变量字段声明没有放在函数体里所以不是语句*为了能从类的外面访问类的字段要用public将其标记为公开的3.字段的初始值提倡声明字段的时候就给字段赋上初始值不赋初始值会获得默认初始值字段数据类型的默认值*字段在声明时初始化和放在构造函数构造器里初始化是完全等价的但是推荐前者因为这样字段的初始值就不受可能会变化的构造函数的影响class Student { 实例字段 public int Age0; || public Student() { this.Age0; } 静态字段 public static int AverageAge100; || static Student() { Student.AverageAge100; } }*构造器有实例构造器和静态构造器前者每创建一个实例就执行一次后者只在加载这个数据类型的时候执行一次永远只执行一次*构造函数的一个功能是给字段初始值实例字段在对象创建、实例构造函数执行时初始化静态字段在类型被加载、静态构造函数执行时初始化*ctor2下tab可以快速插入实例构造函数实例构造器 class Student { public Student(){} } 静态构造器 class Student { static Student(){} }4.只读字段修饰符*只读字段readonly只有一次机会给它赋值只能在类中初始化申明时初始化或在构造函数中初始化两者等价但是不能在类外面给它赋值实例只读字段 class Program { static void Main(sring[] args) { Student stu1new Student(1); Console.WriteLine(stu1.ID); // × stu1.ID2; } } class Student { public rendonly int ID; public Student(int id) { this.ID id; } }静态只读字段 class Program { static void Main(string[] args) { } } struct Color { public int Red; public int Green; public int Blue; } class Brush { public static readonly Color DefaultColor new Color(){Red0,Green0,Blue0}; || public static readonly Color DefaultColor; static Brush() { Brush.DefaultColor new Color(){Red0,Green0,Blue0}; } }属性1.什么是属性class Program { static void Main(string[] args) { try { Student stu1 new Student(); stu1.Age200; Student stu2 new Student(); stu2.Age200; Student stu3 new Student(); stu3.Age200; int avgAge(stu1.Agestu2.Agestu3.Age)/3; Console.WriteLine(avgAge); } catch(Exception ex) { Console.WriteLine(ex.Message); } } } class Student { public int Age //公开的用帕斯卡命名法 { get { return this.Age; } set { if(value0value120) //value为上下文关键字 { this.Agevalue; } else { thorw new Exception(Age value has error!); } } } }2.属性的声明*属性以static修饰时其包装的字段也必须是static1完整声明propfull按2下tab键依次按tab键跳转到下一个可修改处class Program { static void Main(string[] args) { try { Student.Amount -100; Console.WriteLine(Student.Amount); } catch(Exception ex) { Console.WriteLine(ex.Message); } } } class Student { //实例属性 private int age; public int Age { get { return age; } set { if (value 0 value 120) { age value; } else { throw new Exception(Age value has error!); } } } //静态属性 private static int amount; public static int Amount { get { return amount; } set { if (value 0) { amount value; } else { throw new Exception(Amount must greater than 0.); } } } }2简略声明prop按2下tab键与公有字段一样不受保护但写起来非常简单一般用来传递数据的class Student { public int MyProperty { get; set; } }3另外的方法将光标移到字段位置,选择工具栏编辑→重构→封装字段或者快捷键按ctrlRE只读属性只有getclass Student { //不能对其进行赋值 private int age; public int Age { get{return age;} } }还有一种只能在Student类中赋值外界不可访问class Student { private int age; public int Age { get{return age;} private set{agevalue;} } public void SomeMethod() { this.Age20; } }索引器一般情况用在集合里面检索集合int?是可空int类型常量*常量的数据类型只能是基本数据类型例如intdouble等不能用类类型或自定义结构体类型*常量是在编译时就用常量的字面值替换了标识符而静态只读属性要在程序执行时需要访问内存从内存中获取值所以常量的性能比静态只读属性高class Program { static void Main(string[] args) { Console.WriteLine(WASPEC.WebsiteURL); } } class WASPEC { public const string WebsiteURLhttp://www.waspec.org; }成员常量类型的成员局部常量:参与组成方法体中的算法