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. A style question regarding multi-signature methods

A style question regarding multi-signature methods

Scheduled Pinned Locked Moved C#
questiontutorialdiscussion
6 Posts 3 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.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    Hello all. Now that I have discovered extension methods, I am revisiting my libraries of handy-dandy routines to see where it makes sense to convert them. In doing so I am also revisiting a question about how to handle methods that work on different data types. I am wondering if anyone out there has any opinions or knows any arguments for/against writing such methods in a particular style. I know there probably isn't any one "right" answer, but I thought it would be informative to hear what other programmers think. For instance, I have a lot of methods that work on strings. I also need the same operations to work on character arrays. So I might end up with something like

    public int foo ( char[] SourceData ) { ... }
    public int foo ( string SourceData ) { ... }
    

    I have also handled this using

    public int foo ( char[] SourceData ) { return foo(new string(SourceData)); }
    public int foo ( string SourceData ) { ... }
    

    and even

    public int foo ( object SourceData ) 
    {
      string source;
      if ( SourceData.GetType() == typeof(string) ) source = SourceData;
      else if ( SourceData.GetType() == typeof(char[]) ) source = new string(SourceData); 
      else error_condition;
      ...
    }
    

    I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types. Opinions? Critisisms? Other ideas? All are welcome.

    Clive Pottinger Victoria, BC

    P 1 Reply Last reply
    0
    • C Clive D Pottinger

      Hello all. Now that I have discovered extension methods, I am revisiting my libraries of handy-dandy routines to see where it makes sense to convert them. In doing so I am also revisiting a question about how to handle methods that work on different data types. I am wondering if anyone out there has any opinions or knows any arguments for/against writing such methods in a particular style. I know there probably isn't any one "right" answer, but I thought it would be informative to hear what other programmers think. For instance, I have a lot of methods that work on strings. I also need the same operations to work on character arrays. So I might end up with something like

      public int foo ( char[] SourceData ) { ... }
      public int foo ( string SourceData ) { ... }
      

      I have also handled this using

      public int foo ( char[] SourceData ) { return foo(new string(SourceData)); }
      public int foo ( string SourceData ) { ... }
      

      and even

      public int foo ( object SourceData ) 
      {
        string source;
        if ( SourceData.GetType() == typeof(string) ) source = SourceData;
        else if ( SourceData.GetType() == typeof(char[]) ) source = new string(SourceData); 
        else error_condition;
        ...
      }
      

      I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types. Opinions? Critisisms? Other ideas? All are welcome.

      Clive Pottinger Victoria, BC

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      cpotting wrote:

      I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types.

      Why would generics be overkill. Just because you are limiting to only 2 or 3 data types, generics are still very useful. Let's face it, generics (in most cases) are often only used against a couple of types only. Don't limit yourself just because your code doesn't use every possible type.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      L 1 Reply Last reply
      0
      • P Pete OHanlon

        cpotting wrote:

        I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types.

        Why would generics be overkill. Just because you are limiting to only 2 or 3 data types, generics are still very useful. Let's face it, generics (in most cases) are often only used against a couple of types only. Don't limit yourself just because your code doesn't use every possible type.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        Pete O'Hanlon wrote:

        Don't limit yourself

        He may not be. ;)

        led mike

        P 1 Reply Last reply
        0
        • L led mike

          Pete O'Hanlon wrote:

          Don't limit yourself

          He may not be. ;)

          led mike

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          led mike wrote:

          He may not be.

          :laugh: Smooth. You've definitely mastered the one liner.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          C L 2 Replies Last reply
          0
          • P Pete OHanlon

            led mike wrote:

            He may not be.

            :laugh: Smooth. You've definitely mastered the one liner.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles

            C Offline
            C Offline
            Clive D Pottinger
            wrote on last edited by
            #5

            Thanks Pete. I will take a look a closer look at generics. I'm not quite sure what led mike meant by "He may not be", though.

            Clive Pottinger Victoria, BC

            1 Reply Last reply
            0
            • P Pete OHanlon

              led mike wrote:

              He may not be.

              :laugh: Smooth. You've definitely mastered the one liner.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Well I've had two years of learning from some of the best ;)

              led mike

              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