using Object datatype in C#(Disadvantages)

Hi All,

I have a question here, is it good to use Object data type as return type for many methods.

Because of it boxing/unboxing has to be done.I see some performance issues in it.

Am i right or wrong Please throw some light on whether it is good use Object datatype as return type in a method.Its advantages and disadvantages.

Fine if we get some links on msdn too.

Thanks in advance.

Regards,

Arun




Answer this question

using Object datatype in C#(Disadvantages)

  • Kendal Walton

    Read this thread.

    Regards.



  • CruzPedro

    OmegaMan wrote:

    The goal of any code should be performance, but a good design that lends itself extensibility or maintainability that gives understanding by those who pick up the code or must interface with the project, should be considered to trump any boxing/unboxing issues.

    I completely agree. The computer scientist in me always says go for better performance, no matter how little time you are shaving off. However, these days, with the speed and size of processors and RAM, shaving a few microseconds off of a process might not be as important or time saving as a having a well designed and flexible object framework.

    You want to avoid boxing/unboxing yes, absolutely. However when you're dealing with an object-oriented framework, inheritance and polymorphism should be used (if used properly) to their best advantage.

    ---------------
    Tony Alicea
    http://www.theabstractionpoint.com
    "clarity of mind and creativity in application software development..."


  • Ashari Imamuddin

    IMHOPerformance is in the eye of the beholder, if it is a back office application, then most likely the performance is not an issue. Even with winforms...the difference generated may be negligible. I was able to provide complex designs using .Net 1.1 where boxing/unboxing was a fact of life when using the ArrayList before generics were introduced...and the user experience was not significantly impacted.


    With that said, if one can avoid the boxing, then do so, such as using the Generics to handle the objects instead of ArrayList. But if ones design calls for using the base object as a reference then do it.

    For example I have a design where the code uses the base object to pass between functions. It is due to the fact that the consumer creates an enum list. On the individual enums fields each has a shared/known attribute that is read at runtime by my class. The user passes the chosen enum field to the calling function as an object and the code extracts the attribute information off of the object. Why an object For the code does not care what type of enum has been created, it is only interested in the attribute information....hence why an object is passed.

    The goal of any code should be performance, but a good design that lends itself extensibility or maintainability that gives understanding by those who pick up the code or must interface with the project, should be considered to trump any boxing/unboxing issues.

    Thats just my opinion...I could be wrong. <g>


  • DKUS007

    Usually when you see objects being returned in methods, this is a good case to evaluate for use of Inheritance / Interfaces / Generics. In my experience there are mostly edge cases where you can justify returning an object type. True it requires a bit of design to determine the right interface, and to ensure that you create the write implementations for those interfaces, but in the end it makes life much much eaiser. Code is more readable, you get compile time checking, code is easier to debug, and you get performance gains.



  • using Object datatype in C#(Disadvantages)