Extension Methods : Extension « Class « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;


public class MainClass{

   public static void Main(string[] args){   
         String MyString = "a;@b,c:d?e+ hello!";
         int CharCount = MyString.SpecialCharCount();
         Console.WriteLine(CharCount.ToString());


   }
}

   public static class MyStrings
   {
      public static int SpecialCharCount(this String Str)
      {
         var CharQuery = from ThisCount in Str where ThisCount == '!' 
                         || ThisCount == '@'
                         || ThisCount == ';'
                         || ThisCount == ','
                         || ThisCount == ' '
                         select ThisCount;
         return CharQuery.Count<Char>();
      }
   }








7.60.Extension
7.60.1.Adding extension method for int
7.60.2.Adding extension to Stream
7.60.3.Adding reverse operation to string
7.60.4.Add title case to string class
7.60.5.Extension Method On Null Reference
7.60.6.extension method
7.60.7.conflicting extension methods
7.60.8.Extension Methods