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;
    }
    }
    }

  • 相关阅读:
    E
    J
    D
    并查集加优先队列
    动态规划-数位DPwindy
    动态规划-分组背包问题
    动态规划-LIS1
    动态规划-01背包
    [cf1434E]A Convex Game
    [atAGC106F]Figures
  • 原文地址:https://www.cnblogs.com/weihua-show/p/4106342.html
Copyright © 2011-2022 走看看