字段
class Program
{
static void Main(string[] args)
{
List<Student> stuList = new List<Student>();
for (int i = 0; i < 50; i++)
{
Student stu = new Student();
stu.Age = 26;
stu.Score = i;
stuList.Add(stu);
}
int totalAge = 0;
int totalScore = 0;
foreach (var stu in stuList)
{
totalAge += stu.Age;
totalScore += stu.Score;
}
Student.AverageAge = totalAge / Student.Amount;
Student.AverageScore = totalScore / Student.Amount;
Student.ReportAmount();
Student.ReportAverageAge();
Student.ReportAverageSocre();
}
}
class Student
{
public int Age; // 字段 必须在类体里
public int Score; // 非静态字段 实例化后 stu.
public static int AverageAge; // 静态字段 Student.
public static int AverageScore;
public static int Amount;
public Student()
{
Student.Amount++;
}
//readonly 关键字 只在构造时有一次机会修改
public readonly int ID;
public Student(int id)
{
this.ID = id;
}
// 主函数实例化赋值,之后不能再修改
Student stu1 = new Student(1);
Console.WriteLine(stu1.ID);
//
public static void ReportAmount()
{
Console.WriteLine(Student.Amount);
}
public static void ReportAverageAge()
{
Console.WriteLine(Student.AverageAge);
}
public static void ReportAverageSocre()
{
Console.WriteLine(Student.AverageScore);
}
}
readonly示例:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Brush.DefaultColor.Red);
Console.WriteLine(Brush.DefaultColor.Green);
Console.WriteLine(Brush.DefaultColor.Blue);
}
}
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 Program
{
static void Main(string[] args)
{
try
{
Student stu1 = new Student();
stu1.Age = 20;
Student stu2 = new Student();
stu2.Age = 25;
Student stu3 = new Student();
stu3.Age = 280;
int aveAge = (stu1.Age + stu2.Age + stu3.Age) / 3;
Console.WriteLine(aveAge);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
class Student
{
private int age;
public int Age
{
get
{
return this.age;
}
set
{
if (value>=0 && value<=120)
{
this.age = value;
}
else
{
throw new Exception("Age value is error.");
}
}
}
}
快捷键:
属性完整声明:propfull
Tab
键转换
private int amount;
public int Amount
{
get { return amount; }
set {
if(value>=0)
{
this.age = value;
}
else
{
throw new Exception("Amount value is error.");
}
}
}
属性简略声明:prop
public int Score { get; set; }
字段封装快捷键:Ctrl
+R
+E
public int Height { get => height; set => height = value; }
private int height;
索引器
快捷键:ind
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu["Math"] = 90;
var mathScore = stu["Math"];
Console.WriteLine(mathScore);
}
}
class Student
{
private Dictionary<string, int> scoreDictionary = new Dictionary<string, int>();
public int? this[string subject]
{
get {
if (this.scoreDictionary.ContainsKey(subject))
{
return this.scoreDictionary[subject];
}
else
{
return null;
}
}
set {
if(value.HasValue==false)
{
throw new Exception("Score cannot be null");
}
if (this.scoreDictionary.ContainsKey(subject))
{
this.scoreDictionary[subject] = value.Value;
}
else
{
this.scoreDictionary.Add(subject, value.Value);
}
}
}
}
常量
const
例如:PI、int.MaxValue等
隶属于类型,没有实例常量
class Program
{
static void Main(string[] args)
{
Console.WriteLine(BAIDU.WebSite);
}
}
class BAIDU
{
public const string WebSite = "http://www.baidu.com";
}