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

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

    扩展类的用法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using Alearn.ExtensionMethod;
    
    namespace Alearn
    {
        class Program
        {
           
            static void Main(string[] args)
            {
                class1 D = new class1();
                Console.WriteLine(D.add(5,3));
                Console.WriteLine(D.sub(5,3));
                Console.ReadKey();
            }
        }
    }

    已有的类:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Alearn.ExtensionMethod
     8 {
     9    public  class class1
    10     {
    11         public int add(int A, int B)
    12         {
    13 
    14             return (A + B);
    15         }
    16     }
    17 }
    View Code

    扩展方法:

    (注意:1、扩展方法的第一个参数为被扩展的类,要使用this。

              2、扩展方法是一个静态方法。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Alearn.ExtensionMethod
     8 {
     9     public static  class ExtensionCass1
    10     {
    11         public static int sub(this class1 c, int A, int B)
    12         {
    13             return (A - B);
    14         }
    15 
    16     }
    17 }
    View Code
  • 相关阅读:
    关于Table.Compute("Sum(qty)","XXX")的坑
    转载:window.open传值
    一些有用的SQL语句
    VS调试的时候也会锁定SQL对象
    关于数据中时间的坑
    那些年踩过的坑
    一段SQL
    关于下载的问题
    download excle的幾個問題的解決
    《浪潮之巅》读书笔记——第10章 惠普
  • 原文地址:https://www.cnblogs.com/Artemisblog/p/3679953.html
Copyright © 2011-2022 走看看