Receive variable value without passing parameter

Asked

Viewed 717 times

9

I have the following method:

private string toCamelCase(string text)
{
    text = string.Format("{0}{1}",text.Substring(0, 1).ToLower(),text.Substring(1));
    return text;
}

To use it I need to call it that:

toCamelCase("OlaMundo");

I want it to work that way:

"OlaMundo".toCamelCase

2 answers

9


What you want is called extension method. See the wikipedia article. And article in MSDN Magazine (is in VB.NET but is the same thing).

He needs to follow some rules:

  • You need to be in the same namespace of the type you are using as the first parameter (yes, it is a parameter, even if it appears outside the parentheses of the method, it is always so in any method). I don’t need to, but it makes more sense to help tools like Intellisense work better. In fact if you want to use this method only in specific circumstances then it is better to use another name. But the most common is to use the same name as the original type so that this method is always available when the type is used.
  • It needs to be in a static class in addition to the method being static as well. The class can have any name. It is common for programmers to put the suffix Extension or Ext to indicate that it is a class containing extension methods. You can put any static method in there but ideally only put extension methods that are related to each other.
  • You need to have the modifier this before the first parameter that will be used with object to be worked. It is that really determined that the object syntax first can be used.
  • Avoid using types of the first parameter that can generate hierarchies. For example, avoid using too much object, especially if you are in Namespace System. If you do this any kind of data you will count your method as if it were native. This will pollute the table of lookup methods and will make tools like Intellisense not so useful.
  • Be careful not to confuse it with an existing method of the type. The compiler has its rules to decide which one to use but it may not be so clear to the programmer and create bugs slow to realize.
  • Prefer to extend types you have no control over. If you can add a method within the type, do it! So it makes very little sense that it is private. I don’t even know if it works (I think so).

Example:

namespace System {
    public static StringExt {
        public static string ToCamelCase(this string text) {
            return string.Format("{0}{1}", text.Substring(0, 1).ToLower(), text.Substring(1));
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

In the call parentheses are still needed.

"OlaMundo".toCamelCase();

3

Define an extension method (Extension Method) to the string class:

public static string toCamelCase(this string text)
{
    text = string.Format("{0}{1}",text.Substring(0, 1).ToLower(),text.Substring(1));
    return text;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.