zoukankan      html  css  js  c++  java
  • 实例 C#3.0与C#2.0相比之新特性(二)扩展方法

    这是个非常酷的新特性。你可以给方法在编译的时候指定的参数类型。比如说你想添加一个方法,参数类型为string类型。特别值得注意的是一定要在参数前面加上“this”关键字。还是来个Demo吧:

    Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication1
    {
       
        
    public partial class ExtendMethod : System.Web.UI.Page
        {
           
            
    protected void Page_Load(object sender, EventArgs e)
            {
                
    string Name = "qiujuan";
                
    string text=Name.SayHello();
                Response.Write(text);

            } 


        }
        
    //扩展方法必须在顶级的静态类中定义,不能在嵌套类中定义
        public static class ExtendClass
        {
            
    public static string SayHello(this string userName)
            {
                
    return string.Format("Hello,{0}", userName);
            }
        }
    }
  • 相关阅读:
    单链表的反转
    .tar.xz压缩文件的解压
    leetcode Excel Sheet Column Number python
    leetcode Excel Sheet Column Title python
    leetcode Largest Number python
    leetcode Majority Element python
    leetcode Word Break python
    sed命令导致rc.local软链接失效
    Steam内存测试工具
    Ceph pg_num计算
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1369301.html
Copyright © 2011-2022 走看看