C#泛型代理、泛型接口、泛型类型、泛型方法

本文通过具体的C#代码示例展示了如何定义和使用泛型委托及泛型接口,包括基本的泛型概念介绍、泛型委托的声明与使用、泛型接口的实现等。同时,还提供了一个简单的物业费用计算程序来演示这些概念的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//http://www.cnblogs.com/JeffreySun/archive/2012/11/14/2770211.html
//http://www.baqima.com/a/2628.html
//http://www.cnblogs.com/yangqi/archive/2010/07/16/1778767.html
//http://www.cnblogs.com/TianFang/p/3928172.html

using System;
using System.Threading;

namespace Demo
{
    public delegate T3 MyDelegate<T1, T2, T3> (T1 t1, T2 t2);

    interface MyInteface<T1, T2, T3>
    {
        T1 DoIT (T2 t2, T3 t3);
    }


    public class MyClass<T1, T2, T3> : MyInteface<T1, T2, T3>
    {
        public T1 DoIT (T2 t2, T3 t3)
        { 
            //throw new NotImplementedException ();
            T1 t1=default(T1);
            return t1;
        }
    }

    //primary constructor
    //public class User(string name, string password){}

    public class Demo
    {
        private static readonly float KaiFaQu_WuYeFei = 18.8f;

        public static float Pay (int PeopleCount, string Family)
        {
            float result = PeopleCount * KaiFaQu_WuYeFei;
            Console.WriteLine ($"Faimily:{Family} Pay fee {result:c}");
            return result;
        }

        static void Swap<T> (ref T t1, ref T t2)
        {
            T temp = t1;
            t1 = t2;
            t2 = temp;
        }


        public static void Main ()
        {
            String str1 = "abc", str2 = "efg"; 
            Swap<String> (ref str1, ref str2);
            Console.WriteLine ($"{str1}\t{str2}");

            MyDelegate<Int32, String,float> FamXu = new MyDelegate<Int32, String,float> (Pay); 
            MyDelegate<Int32, string,float> FamLiujie = Demo.Pay;

            float MoneyXu = FamXu (4, "XU Minghui");
            float MoenyLiu = FamLiujie (3, "LiuJie");

            //
            MyClass<float,int,string> my =new MyClass<float,int,string>();
            float MoneyMu = my.DoIT (3, "Mu Jingyu");
            Console.ReadKey ();
        }
    }
}

转载于:https://www.cnblogs.com/flaaash/p/5361875.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值