Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do I put my extension method?

A senior member here gave me this code:

public static string Truncate(this string value, int maxChars) {     return value.Length <= maxChars ? value : value.Substring(0, maxChars) + " .."; } 

He said to use it as an extension method. But where do I put this method? It looks like it adds something to .Net

like image 367
Melony Avatar asked Jul 17 '11 16:07

Melony


People also ask

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.

What is the role of an extension method when would you use it?

Extension methods enable developers to add custom functionality to data types that are already defined without creating a new derived type. Extension methods make it possible to write a method that can be called as if it were an instance method of the existing type.

Can you add extension method to an existing static class?

No. Extension methods require an instance variable (value) for an object.

When should we use extension methods 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.


2 Answers

Consider a class named StringExtensions like so:

static class StringExtensions {     public static string Truncate(this string value, int maxChars)     {         return value.Length <= maxChars ?                 value :                 value.Substring(0, maxChars) + " ..";     } } 

Be sure that whatever namespace you put this class in, you include a using declaration for that namespace.

Thus, for a full example:

StringExtensions.cs:

namespace My.Extensions {     static class StringExtensions     {         public static string Truncate(this string value, int maxChars)         {                    return value.Length <= maxChars ?                    value :                    value.Substring(0, maxChars) + " ..";         }     } } 

Program.cs:

using System; using My.Extensions;  namespace My.Program {     static class Program     {         static void Main(string[] args)         {             string s = "Hello, World";             string t = s.Truncate(5);             Console.WriteLine(s);             Console.WriteLine(t);         }     } } 

By the way, you are not adding it to .NET. You are not even adding a new method to the class String. Rather, it's a compiler trick that makes static methods living in static classes with their first parameter declared as this *TypeName* *valueParameter* where *TypeName* is the name of a type, and *valueParameter* is the name of the parameter can be made to appear as an instance method on instances of the type with type name *TypeName*. That is

string t = s.Truncate(5); 

is translated by the compiler into

string t = StringExtensions.Truncate(s, 5); 
like image 99
jason Avatar answered Sep 30 '22 00:09

jason


Put it in a static class, and use using on its namespace.

e.g.

namespace Foo {     static class Extensions     {         public static string Truncate(this string value, int maxChars)         {             return value.Length <= maxChars ?                 value : value.Substring(0, maxChars) + " ..";         }     } } 

And then in a different file:

using Foo;  //Don't forget this!  class Tester {     static void Test()     {         Console.WriteLine("123456".Truncate(3));     } } 
like image 40
user541686 Avatar answered Sep 29 '22 23:09

user541686