方法(Funtions)
来源:互联网 发布:轻声密语知乎 编辑:程序博客网 时间:2024/05/20 07:33
扩展方法(Extension methods (methods with thisbefore their first argument))必须在非泛型的静态类中声明。扩展方法至少有一个参数,而且只有第一个参数能用this关键字标记。静态类本身需具有文件作用域,多个静态类可以定义相同的扩展方法。
注意,扩展方法应谨慎使用1)用一个扩展方法扩展一个类型时,同时也扩展了派生类型;2)扩展方法可能存在版本控制问题。3)扩展方法实际是对一个静态方法的调用,CLR不会对调用方法的表达式的值进行null校验。
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Functions{ // 扩展属性 // Defined in the System.Runtime.CompilerServices namespace // [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly)] // public sealed class ExtensionAttribute : Attribute { // } public static class StringBuilderExtenstion {// public static int IndexOf(StringBuilder sb, char value)// {// for (int index = 0; index < sb.Length; index++)// {// if (sb[index] == value)// {// return index;// }// }// // return -1;// } // 定义StringBuilder类的扩展方法 public static int IndexOf(this StringBuilder sb, char value) { for (int index = 0; index < sb.Length; index++) { if (sb[index] == value) { return index; } } return -1; } } // 定义接口扩展方法 public static class IEnumerableExtenstion { public static void ShowItems<T>(this IEnumerable<T> collection) { foreach (var item in collection) { Console.WriteLine(item); } } } // 定义委托扩展方法 public static class DelegateExtenstion { public static void InvokeAndCatch<TException>(this Action<Object> d, Object o) where TException : Exception { try { d(o); } catch (TException) { Console.WriteLine(/*TException.ToString()*/"TException"); } } }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Functions{ class Program { static void Main(string[] args) { // 1、类中扩展方法 StringBuilder sb = new StringBuilder("Life is what you can make it."); //int index = StringBuilderExtenstion.IndexOf(sb.Replace('i', 'j'), 'j'); //Console.WriteLine(sb); int index = sb.Replace('i', 'j').IndexOf('j'); // 2、接口中扩展方法 "Fighting".ShowItems(); // 隐式类型的数组功能 new[] { "All", "is", "well" }.ShowItems(); new List<int>() { 1, 2, 3 }.ShowItems(); // 3、委托中扩展方法 Action<Object> action = o => Console.WriteLine(o.GetType()); action.InvokeAndCatch<NullReferenceException>(null); Action a = "Dream".ShowItems; a(); Console.ReadKey(); } }}
0 0
- 方法(Funtions)
- Nginx/Tengine服务启动管理脚本(未使用系统funtions函数)
- Funtions in JavaScript
- some notice about slot funtions
- ffmpeg new funtions instead of old functions
- hashCode()方法,equals()方法
- 构造方法(方法重载)
- 静态方法(类方法)与非静态方法(成员方法/实例方法)
- 类方法、实例方法、静态方法(重要)
- 类方法、实例方法、静态方法(重要)
- 类方法、实例方法、静态方法详细诠释(重要)
- 类方法、实例方法、静态方法(重要)
- Python 实例方法、静态方法、类方法 (二十)
- 静态方法+工厂方法(静态方法用途)
- 类方法、实例方法、静态方法(重要)
- js基础知识(公有方法、私有方法、特权方法)
- Java中的类方法与实例方法(对象方法)
- JavaDay05--Notes(方法,方法重载Overload,方法重写Override)
- 哲学家就餐问题——奇数号哲学家先拿起他左手的筷子,然后再拿起他右手的筷子,而偶数号哲学家则先拿起他右手的筷子,然后再拿起他左手的筷子
- 总结
- 深入入门正则表达式(java) - 命名捕获
- 【线性代数公开课MIT Linear Algebra】 第十课 四个子空间
- Cocos2dx解决VS无法加载pdb符号库问题
- 方法(Funtions)
- hdu--1827
- 寻找数组中的第二大数
- 机房收费系统高级数据查询之分组查询基础函数部分
- 优先队列ADT_PrioQueue
- 根据前序,中序遍历重建二叉树
- Floating-Point Numbers
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)
- 作业