locked
Differences between method overloading and Generics RRS feed

  • Question

  • Hi All,

       I am new to C#, would someone share their insight on "differences between method overloading and Generics" .can't one use method overloading instead of Generics.How is Generics understood by the C# compiler at run time.

    Regards
    Sri
    Monday, April 20, 2009 4:38 PM

Answers

  • The short answer is that method overloading is for passing different sets of data to a function.

    Generics are for passing different types to a function.

    For example, say you have a CalculatePay method on an Employee object. You may want to have one function signature that takes no parameters and uses the current date as the "as of" date. You may want a second function signature that takes a date. You may want a third function signature that takes two dates (start date and end date for the pay period).

    Using generics, you may have a CheckForDuplicate method that takes any type of object (Employee, Customer, Lab Result, etc) and compares it against a passed in list. Maybe not the best example, but best I can do for a Monday morning. :-)

    Hope this helps.


    www.insteptech.com
    • Proposed as answer by DeborahKMVP Tuesday, April 21, 2009 3:11 PM
    • Unproposed as answer by srkvellanki Thursday, April 23, 2009 1:50 PM
    • Proposed as answer by Harry Zhu Monday, April 27, 2009 1:24 AM
    • Marked as answer by Harry Zhu Tuesday, April 28, 2009 3:40 AM
    Monday, April 20, 2009 4:47 PM
  • Method overloading is used when the types are known at compile time. 
    For example, look at all of the overloads for Convert.ValueType(PrimitiveType type);

    Generics shine when the type is not known at compile time. 
    For example,  someone wrote collection for the FCL called List<T>. 
    They compiled the code not knowing what type that developers would choose to put into the collection.

    Hope this helps.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Harry Zhu Monday, April 27, 2009 1:24 AM
    • Marked as answer by Harry Zhu Tuesday, April 28, 2009 3:40 AM
    Monday, April 20, 2009 4:50 PM
    Moderator
  • Hi,

    It appears there is no relation between method overloading and Generics.

    In addition to generic methods , we can create generic interfaces, classes, events and delegates, while overloading methods are a handful of methods that take different arguments(you might want to refer to the books about c++ like c++ primier ).

    At run time , the compiler has nothing to do with the application . However , when we build the application, the compiler (csc.exe) will emit assembly (metadata and IL) which will be excuted by CLR at runtime.

    When compiling the code ,real type of generic is unknown. JIT compiler can determine the type of generic when compiling IL code into native CPU instructions.

    Here is some information about generic types in Runtime:
    http://msdn.microsoft.com/zh-cn/library/f4a6ta2h(VS.80).aspx

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Monday, April 27, 2009 1:24 AM
    • Marked as answer by Harry Zhu Tuesday, April 28, 2009 3:40 AM
    Friday, April 24, 2009 3:24 AM

All replies

  • The short answer is that method overloading is for passing different sets of data to a function.

    Generics are for passing different types to a function.

    For example, say you have a CalculatePay method on an Employee object. You may want to have one function signature that takes no parameters and uses the current date as the "as of" date. You may want a second function signature that takes a date. You may want a third function signature that takes two dates (start date and end date for the pay period).

    Using generics, you may have a CheckForDuplicate method that takes any type of object (Employee, Customer, Lab Result, etc) and compares it against a passed in list. Maybe not the best example, but best I can do for a Monday morning. :-)

    Hope this helps.


    www.insteptech.com
    • Proposed as answer by DeborahKMVP Tuesday, April 21, 2009 3:11 PM
    • Unproposed as answer by srkvellanki Thursday, April 23, 2009 1:50 PM
    • Proposed as answer by Harry Zhu Monday, April 27, 2009 1:24 AM
    • Marked as answer by Harry Zhu Tuesday, April 28, 2009 3:40 AM
    Monday, April 20, 2009 4:47 PM
  • Method overloading is used when the types are known at compile time. 
    For example, look at all of the overloads for Convert.ValueType(PrimitiveType type);

    Generics shine when the type is not known at compile time. 
    For example,  someone wrote collection for the FCL called List<T>. 
    They compiled the code not knowing what type that developers would choose to put into the collection.

    Hope this helps.

    Rudedog   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Harry Zhu Monday, April 27, 2009 1:24 AM
    • Marked as answer by Harry Zhu Tuesday, April 28, 2009 3:40 AM
    Monday, April 20, 2009 4:50 PM
    Moderator
  • Hi,

    It appears there is no relation between method overloading and Generics.

    In addition to generic methods , we can create generic interfaces, classes, events and delegates, while overloading methods are a handful of methods that take different arguments(you might want to refer to the books about c++ like c++ primier ).

    At run time , the compiler has nothing to do with the application . However , when we build the application, the compiler (csc.exe) will emit assembly (metadata and IL) which will be excuted by CLR at runtime.

    When compiling the code ,real type of generic is unknown. JIT compiler can determine the type of generic when compiling IL code into native CPU instructions.

    Here is some information about generic types in Runtime:
    http://msdn.microsoft.com/zh-cn/library/f4a6ta2h(VS.80).aspx

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Monday, April 27, 2009 1:24 AM
    • Marked as answer by Harry Zhu Tuesday, April 28, 2009 3:40 AM
    Friday, April 24, 2009 3:24 AM
  • So it appears that none of these replies answered your question?

    If not, could you elaborate on your question so we can better target our replies?
    www.insteptech.com
    Friday, April 24, 2009 3:02 PM