• 【转】C#泛型经典示例(转载)


    泛型是一种特殊的类型,它把指定类型的工作推迟到客户端代码声明并实例化类或方法的时候进行。
    下面是两个经典示例:
    1.输入一个字符串,转化为想要的类型。
    利用泛型的特性,返回值可以是指定的类型。
    2.比较两个对象,返回值较大的一个。
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace FamilyManage
    {
        
    class CGeneric
        {
            
    //数据转换
            static public  T Convert<T>(string s) where T : IConvertible
            {
                
    return (T)System.Convert.ChangeType(s, typeof(T));
            }
            
    //取两个数较大的一个
            static public T Max<T>(T first, T second) where T : IComparable<T>
            {
                
    if (first.CompareTo(second) > 0)
                    
    return first;

                
    return second;
            }
            
    //使用
            static public void test()
            {
                
    //
                int iMax = Max(123456);
                
    double dMax = Max<double>(1.234.56);//可以指定返回类型
                
    //
                int iConvert = Convert<int>("123456");
                
    float fConvert = Convert<float>("123.456");
                
    //
                System.Windows.Forms.MessageBox.Show(iMax + "|" + dMax + "|" + iConvert + "|" + fConvert);
            }
        }
    }
    from:http://www.it100.info/csharp-generic.html
  • 相关阅读:
    栈stack,queue队列
    安装Redis
    为什么负负得正,减负数的意义
    关于数组的记忆
    k8s环境常用操作
    conda python虚拟环境使用locust
    jmeter csv set data中sharing mode的使用说明
    redis常用操作
    数据库基准测试标准 TPC-C or TPC-H or TPC-DS
    influxdb基本操作
  • 原文地址:https://www.cnblogs.com/lzh_527/p/2325545.html
Copyright © 2020-2023  润新知