C#索引样例基础

C基础算法 cyanprobe 9年前 (2015-10-26) 3767次浏览 已收录 1个评论

前言:

现在开始学C#,在学长的帮助下写了个小索引,类之间关系还没弄懂,不过果断还是先发博一下,方便复习巩固。
ful2
 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        { Xuesheng student1 = new Xuesheng();
            student1.dingyi();
            string name = student1[112];
            Console.WriteLine("{0}", name);
            int phone = student1["李四"];
            Console.WriteLine("{0}", phone);
        }
    }
}
 public class Xuesheng
{
    private student[] students;
    public void dingyi()
    {
        students = new student[2];
        students[0] = new student();
        students[0].name = "张三";
        students[0].phone = 112;
        students[1] = new student();
        students[1].name = "李四";
           students[1].phone=110;
    }
   public String this[int thephone]
    {
        get {
            student st = new student() ;
            foreach (student s in students)
            {
                if (thephone==s.phone)
                {
                    st = s;
                }
            }
            return st.name;
        }
    }
    public int this[String thename]
    {
        get {
            student st = new student();
            foreach(student s in students)
            {
                if (thename == s.name)
                {
                    st = s;
                }
                return st.phone;
        }
            return st.phone;
    }
    }
}
public class student
{
    public string name;
    public int phone;
}

在这里实现了一个类数组的索引功能即输入phone输出相对应的学生姓名。


CyanProbe , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#索引样例基础
喜欢 (1)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 有人带就好好学吧,加油!
    Brother Sharp2015-10-26 17:20 回复