Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this extension method not working?

public static string ToTrimmedString(this DataRow row, string columnName)
{
    return row[columnName].ToString().Trim();
}

Compiles fine but it doesn't show up in intellisense of the DataRow...

like image 544
Dean Kuga Avatar asked Mar 23 '11 21:03

Dean Kuga


People also ask

How does extension method work?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

How do you call an extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.

How does extension method work in C#?

In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type.

What is extension method with example?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.


2 Answers

My guess is you haven't included the namespace.

like image 64
gt124 Avatar answered Sep 28 '22 10:09

gt124


Ensure this method is in a static class of its own, separate class from the consuming DataRow.

namespace MyProject.Extensions
{
   public static class DataRowExtensions
   {
      //your extension methods
   }
}

In your consumer, ensure you're:

using MyProject.Extensions
like image 32
p.campbell Avatar answered Sep 28 '22 10:09

p.campbell