zoukankan      html  css  js  c++  java
  • C#扩展方法

    扩展方法:扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。

                  扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。 对于用 C# 和 Visual Basic 编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异。

    下面是NPOI获取Excel单元值的一个扩展方法

    public static class GetStringValue        //静态类
    {
    public static string GetCellValue(this ICell cell)     //this关键字修辞、静态方法   调用的时候直接ICell 类型.GetCellValue()即可
    {
    if (cell == null)
    return string.Empty;
    switch (cell.CellType)
    {
    case CellType.BOOLEAN:
    return cell.BooleanCellValue.ToStr();
    case CellType.NUMERIC:
    if (DateUtil.IsCellDateFormatted(cell))
    {
    return (cell.DateCellValue).ToShortDateString();
    }
    return cell.NumericCellValue.ToStr();
    case CellType.STRING:
    return cell.StringCellValue;
    case CellType.FORMULA:
    return cell.NumericCellValue.ToStr();
    default:
    return string.Empty;
    }
    }
    }

  • 相关阅读:
    codeforces 57C 思维
    FOJ 2232 匈牙利算法找二分图最大匹配
    UVA 101 vector
    POJ 3070 矩阵mob
    codeforces 60B bfs
    codeforces 54A
    codeforces 466C 计数 codeforces 483B 二分 容斥
    FOJ 2213 简单几何
    CCF-最优配餐(BFS)
    杂论-FTP
  • 原文地址:https://www.cnblogs.com/weihua-show/p/4106342.html
Copyright © 2011-2022 走看看