Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generics?

Generics?

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Knechtges
    wrote on last edited by
    #1

    I am not sure how to do this and need some help.... I have a couple of functions that are identical other than one input parameter, signatures look like: public void Func(Dictionary) public void Func(Dictionary) these end up calling the same functions with different signatures, but as I said, do the same thing. What I want is to combine them both into 1 function that handles both cases. Can it be done, and if so how? Thanks!

    A L M 3 Replies Last reply
    0
    • D David Knechtges

      I am not sure how to do this and need some help.... I have a couple of functions that are identical other than one input parameter, signatures look like: public void Func(Dictionary) public void Func(Dictionary) these end up calling the same functions with different signatures, but as I said, do the same thing. What I want is to combine them both into 1 function that handles both cases. Can it be done, and if so how? Thanks!

      A Offline
      A Offline
      Alan Balkany
      wrote on last edited by
      #2

      "What I want is to combine them both into 1 function that handles both cases. Can it be done, and if so how?" Implement the string version, then convert the ints into strings to use it with ints.

      "Microsoft -- Adding unnecessary complexity to your work since 1987!"

      1 Reply Last reply
      0
      • D David Knechtges

        I am not sure how to do this and need some help.... I have a couple of functions that are identical other than one input parameter, signatures look like: public void Func(Dictionary) public void Func(Dictionary) these end up calling the same functions with different signatures, but as I said, do the same thing. What I want is to combine them both into 1 function that handles both cases. Can it be done, and if so how? Thanks!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Maybe. But C# generics aren't C++ templates - you can't call two different overloads of a method based on the concretization of a generic type, because the concretization doesn't happen at compile time. It would look like this:

        public void Func<T>(Dictionary<T, int> somename)

        But going by the description, there's a good chance you can't do this (or actually you could, but you'd have to manually test the type of T, and that's arguably a worse situation than you're on now).

        1 Reply Last reply
        0
        • D David Knechtges

          I am not sure how to do this and need some help.... I have a couple of functions that are identical other than one input parameter, signatures look like: public void Func(Dictionary) public void Func(Dictionary) these end up calling the same functions with different signatures, but as I said, do the same thing. What I want is to combine them both into 1 function that handles both cases. Can it be done, and if so how? Thanks!

          M Offline
          M Offline
          Matt T Heffron
          wrote on last edited by
          #4

          I'd suggest looking at whatever part(s) of the functions that are truly identical (including types) and factoring that logic out of the two specific implementations, into a (private) sub-function. Or use the generic implementation as suggested by Harold. Either way, be careful to avoid falling back to an object-based (non-generic) implementation, because boxing-unboxing will impact your performance.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups