zoukankan      html  css  js  c++  java
  • 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();

            }

        }

    }

  • 相关阅读:
    『开源』仿SQLServer山寨一个 跨数据库客户端
    『开源』也顺手写一个 科学计算器:重磅开源
    『转载』还在找字体?收下这份最全可商用免费字体清单!
    『开源协议』Creative Commons Attribution 3.0 License . 协议的个人理解,并 转载分享 4000个 精美可商用小图标
    『卧槽』意外发现了 Hashtable 的 foreach 用法 BUG
    『性能』List 和 HashSet 查找性能比较 (任何数据量的检索 从此只用 HashSet )
    『备注』GDI+ 绘制文本有锯齿,透明背景文本绘制
    『动态』动态JSON万能转换函数 + .Net40 dynamic动态数据绑定
    『随笔』.Net 底层 数组[] 的 基本设计探秘 512 子数组
    『转载』中国芯片差在哪?这篇讲全了
  • 原文地址:https://www.cnblogs.com/mymindview/p/3610885.html
Copyright © 2011-2022 走看看