Add Extension method : Extension Method « LINQ « C# / CSharp Tutorial






using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
public static class Extensions {
    public static string SpaceToUnderscore(this string source) {
        char[] cArray = source.ToCharArray();
        string result = null;

        foreach (char c in cArray) {
            if (Char.IsWhiteSpace(c))
                result += "_";
            else
                result += c;
        }

        return result;
    }
}
class Program {
    static void Main(string[] args) {
        string s = "This is a test";
        Console.WriteLine(s.SpaceToUnderscore());
    }
}








22.40.Extension Method
22.40.1.Extension method for integer
22.40.2.Add Extension method
22.40.3.Extension Methods Discoverability
22.40.4.Object Initializer