Moslem7026
Moslem7026

Reputation: 3338

Why is my extension method not found?

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?

Upvotes: 1

Views: 3706

Answers (4)

adt
adt

Reputation: 4360

Extension methods should be written in static classes.

Example

public static class StringExtension
{
    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;
    }
}

Upvotes: 0

Mario S
Mario S

Reputation: 11955

Without changing the code in your extension method, add the extension method to a static class:

public static class MyExtensions // Name this class to whatever you want, but make sure it's static.
{
    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;
    }
}

Then call it like so:

string name = "moslem";
string underscoreName = name.SpaceToUnderScore(); // Omit the name parameter (prefixed with this) when called like this on the string instance. 

// This would work to:
string underscoreName = MyExtentions.SpaceToUnderScore(name);

If you don't find the extension method, make sure you are using the namespace of the static class.

Upvotes: 0

codingbiz
codingbiz

Reputation: 26396

public static class MyStringExtenstionClass
{

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

and like this

 string name = "John Doe";
 name = name.SpaceToUnderScore(); //no argument and called on instance of string

Upvotes: 0

L.B
L.B

Reputation: 116178

First put your extension method to a static class then invoke as name.SpaceToUnderScore()

var newstr = "a string".SpaceToUnderScore();


public static class SomeExtensions
{
    public static string SpaceToUnderScore(this string source)
    {
        return new string(source.Select(c => char.IsWhiteSpace(c) ? '_' : c).ToArray());
        //or
        //return String.Join("",source.Select(c => char.IsWhiteSpace(c) ? '_' : c));
    }
}

Upvotes: 3

Related Questions