C#基础_17 属性,字段,索引器,常量

字段

	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";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值