CSharp: Collection

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

namespace CSharpDataStructuresAlgorithms
{

    /// <summary>
    /// 直接存取群集 Collection
    /// geovindu,Geovin Du
    /// </summary>
    public struct Name
    {

        /// <summary>
        /// 
        /// </summary>
        private string fname, mname, lname;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="first"></param>
        /// <param name="middle"></param>
        /// <param name="last"></param>
        public Name(string first, string middle, string last)
        {
            fname = first;
            mname = middle;
            lname = last;
        }
        /// <summary>
        /// 
        /// </summary>
        public string firstName
        {
            get
            {
                return fname;
            }
            set
            {
                fname = firstName;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string middleName
        {
            get
            {
                return mname;
            }
            set
            {
                mname = middleName;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string lastName
        {
            get
            {
                return lname;
            }
            set
            {
                lname = lastName;
            }
        }
        /// <summary>
        /// 全名
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return (String.Format("{0} {1} {2}", fname, mname, lname));
        }
        /// <summary>
        /// 缩写名
        /// </summary>
        /// <returns></returns>
        public string Initials()
        {
            return (String.Format("{0}{1}{2}", fname.Substring(0, 1), mname.Substring(0, 1), lname.Substring(0, 1)));
        }
    }

}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Security.Cryptography.X509Certificates;

namespace CSharpDataStructuresAlgorithms
{

    /// <summary>
    /// Collection 群集
    /// </summary>
    public class DuCollection: CollectionBase
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void Add(object item)
        {

            InnerList.Add(item);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        public void Romove(object item)
        {
            InnerList.Remove(item);
        }
        /// <summary>
        /// 
        /// </summary>
        public new void Clear()
        { 
            InnerList.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Contains(object item)
        {
            return InnerList.Contains(item);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int IndexOf(object item)
        {
            return InnerList.IndexOf(item);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public new int Count()
        { 
            return InnerList.Count; 
        
        }

    }
}

  

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

namespace CSharpDataStructuresAlgorithms
{

    /// <summary>
    /// 
    /// </summary>
    public class Timing
    {
        TimeSpan startingTime;
        TimeSpan duration;
        /// <summary>
        /// 
        /// </summary>
        public Timing()
        {
            startingTime = new TimeSpan(0);
            duration = new TimeSpan(0);
        }
        /// <summary>
        /// 
        /// </summary>
        public void StopTime()
        {
            duration =
            Process.GetCurrentProcess().Threads[0].
            UserProcessorTime.Subtract(startingTime);

        }
        /// <summary>
        /// 
        /// </summary>
        public void StartTime()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            startingTime =
            Process.GetCurrentProcess().Threads[0].
            UserProcessorTime;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public TimeSpan Result()
        {
            return duration;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="arr"></param>
        public static void BuildArray(int[] arr)
        {
            for (int i = 0; i < 100000; i++)
                arr[i] = i;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="arr"></param>
        public static void DisplayNums(int[] arr)
        {
            for (int i = 0; i <= arr.GetUpperBound(0); i++)
                Console.Write(arr[i] + " ");
        }

    }

}

  

调用:、

Console.WriteLine("Hello, Geovin Du!");
Name myName = new Name("Michael", "Mason", "McMillan");
string fullName, inits;
fullName = myName.ToString();
inits = myName.Initials();
Console.WriteLine("My name is {0}.", fullName);
Console.WriteLine("My initials are {0}.", inits);
int[] nums = new int[100000];
Timing.BuildArray(nums);
Timing tObj = new Timing();
tObj.StartTime();
Timing.DisplayNums(nums);
tObj.StopTime();
Console.WriteLine("time (.NET): " + tObj.Result().TotalSeconds);

  输出:

Hello, Geovin Du!
My name is Michael Mason McMillan.
My initials are MMM.

  

posted @ 2023-01-27 14:49  ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报