Is there any success in creating extension methods that work with an object type?

I have a set of extension methods that I regularly use for various user interface tasks. I usually define them to run an object of type, although inside them I usually convert them to string types.

public static string FormatSomething(this object o) { if( o != null ) { string s = o.ToString(); /// do the work and return something. } // return something else or empty string. } 

The main reason I use an object type, not a string, is to save myself in the user interface because of the need to do <%#Eval("Phone").ToString().FormatSomething()%> when I can do <%#Eval("Phone").FormatSomething()%> instead.

So, is it good in terms of performance to create all the extension methods for an object, or should I convert them to string (or corresponding) types based on what the extension method does?

+19
c # extension-methods
04 Oct 2018-11-18T00:
source share
4 answers

Is there any performance for creating extension methods that work with an object type?

Yes. If you pass a value type, then the value type will be indicated. This creates a performance limitation when distributing the field and executing the copy, and also, of course, later, to garbage collect the box.

Instead

 public static string FormatSomething(this object o) { return (o != null) ? o.ToString() : ""; } 

I would write

 public static string FormatSomething<T>(this T o) { return (o != null) ? o.ToString() : ""; } 

This has the same effect, but avoids the penalty for boxing. Rather, he trades a penalty for a bonus for each penalty for the first penalty for jing.

Is it good in terms of performance to create all extension methods on an object?

We cannot answer the question. Give it a try! Measure productivity, compare it with the desired performance and make sure that you have reached your goal. If so, excellent. If not, use the profiler, find the slowest thing and fix it.

But not a single question is a question that you must ask. The question you should ask:

Good programming practice to create an extension method that extends everything?

No. This is almost never a good idea. In most cases, when people want to do this, they abuse the mechanism of the extension method. Typically, there is a more specific type that can be extended. If you do this a lot, then you get a lot of extension methods for each type, and the coding becomes confusing and error prone.

For example, suppose you want to have an extension method that answers the question "does this sequence contain this value?" You can write:

 public static bool IsContainedIn<T>(this T item, IEnumerable<T> sequence) 

and then say

 if (myInt.IsContainedIn(myIntSequence)) 

But it’s much better to say:

 public static bool Contains<T>(this IEnumerable<T> sequence, T item) 

and then say

 if (myIntSequence.Contains(myInt)) 

If you do this in the first way, then you print to the IDE, and every time you enter ".", You get a request with IsContainedIn as an option, because maybe you are going to write code that determines if this object is in collection. But in 99% of cases you will not do it. This adds noise to the instrument and makes it difficult to find what you really want.

+42
04 Oct '11 at 18:24
source share

I seriously doubt that the consequences of any work with the IDE will have performance implications. After compilation, I did not expect this to make a difference.

+2
04 Oct '11 at 18:03
source share

Compared to when you call ToString to actually call FormatSomething (you have a null check, it may take a few more ms, but they also make the code more reliable.

Even if the type of compilation time of the object that you call with the on method was a string, it will still have a noticeable difference.

Don't worry about performance until you have a performance issue. Until then, worry about maintainability, including readability. If you have a performance issue, use the profiler to find where it is.

+2
04 Oct '11 at 18:18
source share

What overhead is associated with the extension method at runtime? (.NET) answers your question, I think. Extension methods are only static methods, so they are not of type Object. Intellisense only does that.

+1
04 Oct 2018-11-18T00:
source share



All Articles