Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:13:28+00:00 2026-05-26T01:13:28+00:00

I have a set of extension methods that I regularly use for various UI

  • 0

I have a set of extension methods that I regularly use for various UI tasks. I typically define them to run off of type object, even though inside of them I’m typically converting 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 type object and not string is to save myself in the UI from having to do <%#Eval("Phone").ToString().FormatSomething()%> when I can do <%#Eval("Phone").FormatSomething()%> instead.

So, is it fine from performance standpoint to create all the extension methods on object, or should I convert them to be string (or relevant) types based on what the extension method is doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:13:28+00:00Added an answer on May 26, 2026 at 1:13 am

    Is there a performance hit for creating extension methods that operate off the object type?

    Yes. If you pass a value type in then the value type will be boxed. That creates a performance penalty of allocating the box and doing the copy, plus of course later having to garbage collect the box.

    Instead of

    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() : "";
    }
    

    That has the same effect, but avoids the boxing penalty. Or rather, it trades a per call boxing penalty for a first call jitting cost penalty.

    is it fine from performance standpoint to create all the extension methods on object?

    We cannot answer the question. Try it! Measure the performance, compare that against the desired performance, and see if you met your goal. If you did, great. If not, use a profiler, find the slowest thing, and fix it.

    But neither question is the question you should be asking. The question you should have asked is:

    Is it a good programming practice to create an extension method that extends everything?

    No. It is almost never a good idea. In most cases where people want to do that, they are abusing the extension method mechanism. Typically there is some more specific type that could be extended. If you do this a lot then you end up with lots of extension methods on every type, and 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 could write:

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

    and then say

    if (myInt.IsContainedIn(myIntSequence))
    

    But it is 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 it the first way then you’re typing along in the IDE and every single time you type “.”, you get prompted with IsContainedIn as an option because maybe you’re about to write code that determines if this object is in a collection. But 99% of the time, you’re not going to do that. Doing this adds noise to the tooling and makes it harder to find what you really want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SOAP result set that the nuSoap extension has turned into a
I have a class that modifies data via some extension methods. In order to
I have a Python set that contains objects with __hash__ and __eq__ methods in
Imagine I have a set of global methods that aren't associated with any specific
I have a set of files in a single directory named: OneThing-x.extension OneThing-y.extension OneThing-z.extension
I have set up a Django application that uses images. I think I have
I have set of scripts for doing scripted installs. You can use the scripts
I have a problem with my code posted below, set and get methods. I
I made a simple extension of CheckBoxPreference so that I could have my own
I'm wanting to use an extension for Python that I found here , but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.