在C#语言中,类(或结构体)包含以下成员:

internal class Program{static void Main(string[] args){// 实例化一个对象Student stu1 = new Student();stu1.Age = 40;stu1.Score = 90;Student stu2 = new Student();stu2.Age = 24;stu2.Score = 60;Student.ReportAmount(); // >>>2}}class Student{// 定义实例字段(只有通过实例化之后才能被调用,不能通过类名调用)public int Age;public int Score;// 定义静态字段(通过类名调用,不能通过实例调用)public static int AverageAge;public static int AverageScore;public static int Amount;public Student(){Student.Amount++;}public static void ReportAmount(){Console.WriteLine(Student.Amount);}}
internal class Program{static void Main(string[] args){List students = new List();for (int i = 0; i < 100; i++){Student student = new Student();student.Age = 24;student.Score = i;students.Add(student);}int totalAge = 0;int totalScore = 0;foreach (Student student in students){totalAge += student.Age;totalScore += student.Score;}Student.AverageAge = totalAge / Student.Amount;Student.AverageScore = totalScore / Student.Amount;Student.ReportAmount(); // >>>100Student.ReportAverageAge(); // >>>24Student.ReportAverageScore(); // >>>49}}class Student{// 定义实例字段(只有通过实例化之后才能被调用,不能通过类名调用)public int Age;public int Score;// 定义静态字段(通过类名调用,不能通过实例调用)public static int AverageAge;public static int AverageScore;public static int Amount;public Student(){Student.Amount++;}public static void ReportAmount(){Console.WriteLine(Student.Amount);}public static void ReportAverageAge(){Console.WriteLine(Student.AverageAge);}public static void ReportAverageScore(){Console.WriteLine(Student.AverageScore);}}
public int Age; // 访问级别 变量类型 变量名;
public static int Amount; // 访问级别 static 变量类型 变量名;
// 建议在声明字段时就进行初始化
// 只读实例字段readonly
internal class Program{static void Main(string[] args){Student student = new Student(1);Console.WriteLine(student.ID); // >>>1student.ID = 2; // >>>无法编译,不能修改只读字段}}class Student{// 定义实例字段(只有通过实例化之后才能被调用,不能通过类名调用)public readonly int ID; // 只读实例字段public int Age;public int Score;// 定义静态字段(通过类名调用,不能通过实例调用)public static int AverageAge;public static int AverageScore;public static int Amount;public Student(int ID) // 动态初始化器,每次实例化都会执行{this.ID = ID; // 初始化只读字段}}
// 静态只读字段
internal class Program
{static void Main(string[] args){Console.WriteLine(Brush.DefaultColor.Red); // >>>0Console.WriteLine(Brush.DefaultColor.Green); // >>>0Console.WriteLine(Brush.DefaultColor.Blue); // >>>0}
}
struct Color
{public int Red;public int Green;public int Blue;
}class Brush
{public static readonly Color DefaultColor = new Color() { Red = 0, Green = 0, Blue = 0 };// 上面的语句和下面的作用是一致的public static readonly Color DefaultColor;static Brush(){Brush.DefaultColor = new Color() { Red = 0, Green = 0, Blue = 0 };}
}
class Student{// 定义实例字段(只有通过实例化之后才能被调用,不能通过类名调用)public int Age;public int Score;// 定义静态字段(通过类名调用,不能通过实例调用)public static int AverageAge;public static int AverageScore;public static int Amount;public Student() // 动态初始化器,每次实例化都会执行{}static Student() // 静态初始化器,只在类加载时被执行{}}
// 字段怎样向属性进化的??
// 将字段的访问级别由public改为private
// 变量名使用小写字母age
// 定义GetAge()和SetAge()方法,分别获取和设置属性值
internal class Program{static void Main(string[] args){try{Student stu1 = new Student();Student stu2 = new Student();Student stu3 = new Student();stu1.SetAge(20);stu2.SetAge(20);stu3.SetAge(200);double AverageAge = (stu1.GetAge() + stu2.GetAge() + stu3.GetAge()) / 3;Console.WriteLine(AverageAge);}catch (Exception ex){Console.WriteLine(ex.Message);}}}class Student{private int age;public int GetAge(){ return this.age; }public void SetAge(int age){if (age > 0 & age < 100){this.age = age;}else{throw new Exception("Age value has error!!");}}}
// 在C#中属性怎么创建??
// 定义名为Age的public变量
// 在变量名后加入{get{} set{}}
internal class Program{static void Main(string[] args){try{Student stu1 = new Student();Student stu2 = new Student();Student stu3 = new Student();stu1.Age = 20;stu2.Age = 20;stu3.Age = 20;double AverageAge = (stu1.Age + stu2.Age + stu3.Age) / 3;Console.WriteLine(AverageAge);}catch (Exception ex){Console.WriteLine(ex.Message);}}}class Student{public int Age{get{return this.Age;}set{if (value > 0 & value < 100) // value是上下文关键字,在使用set时,value代指用户输入的指{this.Age = value;}else{throw new Exception("Age value has error!!");}}}}
private int myVar;
public int MyProperty
{get { return myVar; }set { myVar = value; }
}
public int MyProperty { get; set; }
internal class Program{static void Main(string[] args){Student student = new Student();var mathScore = student["Math"];Console.WriteLine(mathScore==null); // >>>turestudent["Math"] = 90;var newMathScore = student["Math"];Console.WriteLine(newMathScore); // >>>90}}class Student{// 声明一个名为scoreDictionary的字典private Dictionary scoreDictionary = new Dictionary();// 创建索引器,快捷方式indexer+两下tab// int?表示可以是空值public int? this[string subject]{get { /* return the specified index here */ if (this.scoreDictionary.ContainsKey(subject)){return scoreDictionary[subject];}else{return null;}}set { /* set the specified index to value here */if (value.HasValue == false){throw new Exception("Score cannot be null");}if (this.scoreDictionary.ContainsKey(subject)){scoreDictionary[subject] = value.Value;}else{this.scoreDictionary.Add(subject, value.Value);}}}}
public const double PI;