using generic types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SoftwareTester
    New Member
    • Jan 2008
    • 13

    using generic types

    I have a function like
    Code:
    UInt32[] MyFunction(UInt32[] Inputs, bool bOrder)
    Instead of rewriting the whole function for UInt64[] and for string[] I would like to write it in a generic way using something like
    Code:
    <T>[] MyFunction(<T>[] Inputs, bool bOrder)
    Is this possible in C# and what problems can I expect while using it?
    Last edited by pbmods; Feb 28 '09, 11:26 PM. Reason: Added CODE tags.
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    It should work fine. For an individual method, you would declare:
    Code:
    T[] MyFunction<T>(T[] Inputs, bool bOrder){...}
    or for a class you would declare:
    Code:
    class MyClass<T>{
    T[] MyFunction(T[] Inputs, bool bOrder){..}
    }

    Comment

    • mldisibio
      Recognized Expert New Member
      • Sep 2008
      • 191

      #3
      Also, look into the Array.Sort<T>(T[], IComparer<T>) if all you want to do is sort a generic array with a specified direction.

      Comment

      Working...