Add Extension method : Extension Method « LINQ « C# / C Sharp






Add Extension method

 
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());
    }
}

 








Related examples in the same category

1.Extension method for integer
2.Extension Methods Discoverability
3.Object Initializer