C#-----扩展方法

    xiaoxiao2021-04-01  33

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { static class Program //必须是静态类才可以添加扩展方法 { static void Main(string[] args) { string str = "ytu"; Console.WriteLine(str.add());//调用扩展方法,必须用对象来调用 Console.ReadKey(); } //声明扩展犯非法,扩展方法必须是静态的 //add有三个参数,this必须有,string表示要扩展的对象,stringName表示对象名 //三个参数this和扩展的类型必不可少,对象名可以自己任意取 //如果需要传递参数,在增加一个变量即可 public static string add(this string stringName) { return stringName + "afdfdsa"; } } }
    转载请注明原文地址: https://ju.6miu.com/read-665494.html

    最新回复(0)