惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
B
Blog
博客园 - Franky
H
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
云风的 BLOG
云风的 BLOG
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
V
V2EX
Martin Fowler
Martin Fowler
T
Tailwind CSS Blog
有赞技术团队
有赞技术团队

博客园 - 花****半

lr三大组件关系图 C#----继承 实验2 -DataList 增删改查等操作 实验1----GridView 增删改查 开发实训 发生类型为 System.OutOfMemoryException 的异常。请问这是什么原因 进度条的问题 学习ajax-二级联动下拉列表 - 花****半 - 博客园 SharpZipLib 怎么提高压缩比? 2月14号 句话是什么意思?if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PerPer SQL Server中的sysobjects” 是什么意思 在sysobjects表中,xtype=rf ASP好东西!所以 —共享 target 怎么把一个页面的数据传到另一个页面? 提示以下的错误信息:“未能在设计视图中打开,"" 块中,以不同方式将值括起来 ” 什么问题这是? split
C#-----属性和索引器
花****半 · 2009-03-25 · via 博客园 - 花****半

  属性介绍:

属性是对成员变量的读写之前进行验证,看条件是否符合,主要针对变量是普通类型的情况

索引器也是完成验证的过程,只不过它主要是针对变量是数组或集合的情况,通过索引器下标找到给数组或集合里面的哪个变量进行赋值或读值。

class   Student

    {

        public int nAge = 1000;

        //年龄属性

        public int Age

        {

            get

            {

                return nAge;

            }

            set

            {

                if (value < 100)

                {

                    nAge = value;

                }

                else

                {

                    Console.WriteLine("年龄不符合要求");

                }

            }

        }

        static void Main(string[] args)

        {

            Student obj = new Student();

            int num = obj.nAge;

            Console.WriteLine(obj.nAge);

            Console.WriteLine(num);

            obj.Age = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(obj.Age);

            Console.Read();

        }

索引器示例一

class Program

    {

        /// <summary>

        /// 这是一个控制台程序

        /// </summary>

        private string[] arr;

        public string[] aa = new string[3];

        public Program()

        {

            arr = new string [4];

            for (int i = 0; i < arr.Length; i++)

            {

                arr[i] = "张三";

            }

        }

        public string this[int index]

        {

            get { return arr[index]; }

            set { arr[index] = value; }     

        }

        public string this[string str]

        {

            get

            {

                int count = 0;

                for (int i = 0; i < arr.Length; i++)

                {

                    if (arr[i] == str)

                    {

                        count++;

                    }

                }

                return count.ToString();

            }

            set

            {

                for (int i = 0; i < arr.Length; i++)

                {

                    if (arr[i] == str)

                    {

                        arr[i] = value;

                    }

                }

            }

        }

        static void Main(string[] args)

        {

            Program pr = new Program();

           //写入部分值

            pr[0] = "wangwu";

            pr[2] = "liuba";

            for (int i = 0; i <pr.arr.Length; i++)

            {

                Console.WriteLine(pr[i]);

            }

            //读取值

            pr["张三"] = "张三都被替换了";

            for (int i = 0; i < pr.arr.Length; i++)

            {

                Console.WriteLine(pr[i]);

            }

            Console.WriteLine(pr["张三"]);

            Console.Read();

        }

    }