I want to add a method to string that convert space char to underscore (extension method), I deployed the code but why it doesn’t work?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string name = "moslem";
string.SpaceToUnderScore(name);
}
public static string SpaceToUnderScore(this string source)
{
string result = null;
char[] cArray = source.ToArray();
foreach (char c in cArray)
{
if (char.IsWhiteSpace(c))
{
result += "_";
}
else
{
result += c;
}
}
return result;
}
}
}
Why doesn’t it work?
First put your extension method to a static class then invoke as
name.SpaceToUnderScore()