码迷,mamicode.com
首页 > Windows程序 > 详细

C#泛型在委托Delegate上的实现

时间:2015-05-11 07:44:48      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace ConsoleApplication4
{
    delegate T NumberChanger<T>(T n);
   
    class Program
    {
        static int num = 10;
        public static int AddNum(int p)
        {
            num += p;
            return num;
        }


        public static int MultNum(int p)
        {
            num *= p;
            return num;
        }

        static void Main(string[] args)
        {
            MyGenericArray<int,char> intArray = new MyGenericArray<int,char>(5);
            for (int c = 0; c < 5; c++)
            {
                intArray.SetItem(c, c * 5);
            }
            for (int c = 0; c < 5; c++)
            {
                Console.Write(intArray.GetItem(c)+" ");
            }
            intArray.GenericMethod<string>("heoooooo");
            intArray.GenericMethod<int>(100);
            MyGenericArray<char,string > charArray = new MyGenericArray<char,string >(5);
            for (int c = 0; c < 5; c++)
            {
                charArray.SetItem(c, (char)(c+97));
            }
            for (int c = 0; c < 5; c++)
            {
                Console.Write(charArray.GetItem(c) + " ");
            }
            int a, b;
            char cc, d;
            a = 10;
            b = 20;
            cc = I;
            d = v;
            Console.WriteLine("a:{0};b:{1}",a,b);

            Console.WriteLine("cc:{0};d:{1}", cc, d);
            Swap<int>(ref a, ref b);
            Swap<char>(ref cc, ref d);
            Console.WriteLine("a:{0};b:{1}", a, b);

            Console.WriteLine("cc:{0};d:{1}", cc, d);

            NumberChanger<int> nc1 = new NumberChanger<int>(AddNum);
            NumberChanger<int> nc2 = new NumberChanger<int>(MultNum);
            nc1(25);
            Console.WriteLine(num);
            nc2(5);
            Console.WriteLine(num);

            Console.ReadKey();
        }
        public static void Swap<T>(ref T lhs, ref T rhs)
        {
            T temp;
            temp = lhs;
            lhs = rhs;
            rhs = temp;

        }
    }
    public class MyGenericArray<T,K>  where T:struct

    {
        private T[] array;

        public MyGenericArray(int size)
        {
            array = new T[size + 1];
        }

        public T GetItem(int index)
        {
            return array[index];
        }

        public void SetItem(int index, T value)
        {
            array[index] = value;
        }
        public void GenericMethod<X>(X x)
        {
            Console.WriteLine(x.ToString());
        }
    }
    //public : MyGenericAr《y <int>
    //{ 
        
    //}
    //public class SubGenerClass<T>:MyGenericArray <T> where T;struct
    //{
        
    //}
}

 

C#泛型在委托Delegate上的实现

标签:

原文地址:http://www.cnblogs.com/heisaijuzhen/p/4493555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!