码迷,mamicode.com
首页 > Windows程序 > 详细

C#知识点

时间:2015-06-16 22:51:08      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

1.属性

//属性的2种写法
public class person
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name=value;
        }
    }
    public int Age
    {
        get;
        set;
    }
} 

 2.索引器

//外部调用
person p=new person;
sting str=p[0]
public class person
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name=value;
        }
    }
    public int Age
    {
        get;
        set;
    }
        public string Email
    {
        get;
        set;
    }
//也可以用 sting 键值,也可以多个参数
//可以重载,PS:public string this[int index],public string this[string key]
    public string this[int index]
    {
        get
        {
            string result="";
            switch(index)
            {
                case 0:
                result=this.Name;
                break;
                case 1:
                result=this.Age;
                break;
                case 2:
                result=this.Email;
                break;
            }
            return result;
        }
    }
} 

 

  

C#知识点

标签:

原文地址:http://www.cnblogs.com/milest/p/4581860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!