首页 > 代码库 > C#数据结构学习

C#数据结构学习

Collection类学习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Colloction
{
    class Collection:CollectionBase
    {
        public void Add(Object item) { InnerList.Add(item); }
        public void Remove(Object item) { InnerList.Remove(item); }
        public void Clear(){InnerList.Clear();}
        public int Count() { return InnerList.Count; }
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            Collection names = new Collection();
            names.Add("1");
            names.Add("2");
            names.Add("3");
            names.Add("4");
            foreach (Object name in names) { Console.WriteLine(name); }
            Console.WriteLine("总数量: "+names.Count());
            names.Remove("3");
            Console.WriteLine("总数量: " + names.Count());
            names.Clear();
            Console.WriteLine("总数量: " + names.Count());            
        }
    }
}

 泛型

 static void Swap<T>(ref T val1,ref T val2)
        {
            T temp;
            temp = val1;
            val1 = val2;
            val2 = temp;
        }
        static void Main(string[] args)
        {
            int num1 = 100;
            int num2 = 200;
            Swap<int>(ref num1, ref num2);
            Console.WriteLine(num1);
        }

 测量时间

            DateTime startTime;
            TimeSpan endTime;
            startTime = DateTime.Now;
            for (int i = 0; i < 10000000; i++)
                ;
            endTime = DateTime.Now.Subtract(startTime);
            Console.WriteLine(endTime.TotalSeconds);            

 

C#数据结构学习