• C#之泛型类与泛型方法


    namespace ConsoleApplication1
    {
        class Program
        {
            public Program() { }
            public void  swap<T>(ref T x, ref T y)
            {
                T m;
                m = x;
                x = y;
                y = m;
            }
          
            /*泛型方法*/
            static void Main(string[] args)
            {
                Program pro = new Program();
                string str1 = "zadk";
                string str2 = "zbc";
                pro.swap(ref str1,ref str2);
                Console.WriteLine("{0},{1}",str1,str2);
                Console.Read();
            }
        }
    }

    集合类List的用法

    static void Main(string[] args)         {            

    List<string> item = new List<string>();           

      item.Add("清华同方");            

    item.Add("IBM");           

      item.Insert(0, "lenov");            

    string[] title = { "Tecent", "华硕" };            

    item.AddRange(title);            

    Console.WriteLine("起始列表项:");           

      foreach (string str in item) {                   Console.Write("{0} ",str);             }            

    Console.WriteLine("list中共有{0}项",item.Count);           

      Console.WriteLine("查找指定元素huashuo:{0}", item.IndexOf("huashuo"));           

      Console.WriteLine("查找指定元素Tecent:{0}", item.IndexOf("Tecent"));            

    Console.WriteLine("删除指定元素'清华同方后'元素列表为:");          

       item.Remove("清华同方");             foreach (string str in item)           

      {                 Console.Write("{0} ", str);             }            

    item.Clear();           

      foreach (string str in item)           

      {                 Console.Write("{0} ", str);             }           

      Console.Read();

            }

    综合用

    namespace ConsoleApplication1

    {

        publicclassContact {

            public Contact() { }

            publicstring name { get; set; }

            publicstring phone { get; set; }

        }

        classProgram

        {

            public Program() { }

            staticvoid Main(string[] args)

            {

                string[] sname = { "Jerry","Mary","Angela","Liminhao"};

                string[] sphone = { "11","22","33","44"};

                List<Contact> person = newList<Contact>();

                Contact con = null;

                String search = "";

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

                {

                    con = newContact();

                    con.name = sname[i];

                    con.phone = sphone[i];

                    person.Add(con);

     

                }

                Console.WriteLine("=======================================");

                Console.WriteLine("******按姓名查询号码*************");

                do

                {

                    Console.WriteLine("请输入要查询人电话,以q键结束");

                    search = Console.ReadLine();

                    foreach (Contact item in person)

                    {

                        if (item.phone == search)

                        {

                            Console.WriteLine("电话是{0}的人姓名是{1}", search, item.name);

                        }

                    }

     

                } while (search != "q");

                    Console.Read();

            }

        }

    }

  • 相关阅读:
    为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
    Weblogic12c安装与配置详解
    淘宝自己的前端框架KISSY(类似jquery)
    ExtJS4中initComponent和constructor的区别
    Servlet的getContextPath(), getServletPath(), getRequestURI(), getRealPath("/")
    Hash Join 一定是选择小表作为驱动表吗
    oracle for loop 简单
    oracle正则表达式
    Android开发--用户定位服务--UserLocation
    android蓝牙开发---与蓝牙模块进行通信
  • 原文地址:https://www.cnblogs.com/mymindview/p/3610885.html
Copyright © 2020-2023  润新知