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. How to do math operations with generic function arguments?

How to do math operations with generic function arguments?

Scheduled Pinned Locked Moved C#
c++helptutorialquestion
3 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
    Chesnokov Yuriy
    wrote on last edited by
    #1

    T will be some value type int, float, double, etc... public T Mean(T[] vec) where T : ??????? { T tmp = 0; <--- error foreach (T v in vec) tmp += v; <--- error return tmp / vec.Length; <--- error } Are they so complex compared to C++

    chesnokov

    C N 2 Replies Last reply
    0
    • C Chesnokov Yuriy

      T will be some value type int, float, double, etc... public T Mean(T[] vec) where T : ??????? { T tmp = 0; <--- error foreach (T v in vec) tmp += v; <--- error return tmp / vec.Length; <--- error } Are they so complex compared to C++

      chesnokov

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      Sad but true: there is (not yet) a INumber or IArithmetic interface for this. So either you have to use reflection to query the operators or you have to define a Interface yourself. With .net 3.5 you can use the extension-method system to add the implementation for your interface to double/int/etc. so maybe this is the best way.

      1 Reply Last reply
      0
      • C Chesnokov Yuriy

        T will be some value type int, float, double, etc... public T Mean(T[] vec) where T : ??????? { T tmp = 0; <--- error foreach (T v in vec) tmp += v; <--- error return tmp / vec.Length; <--- error } Are they so complex compared to C++

        chesnokov

        N Offline
        N Offline
        NassosReyzidis
        wrote on last edited by
        #3

        try this : public T Mean(T[] vec) where T : Int32,Double,Fload,..... { tmp = default(T); //Return the default for each Primitive, for No primitive this is equal to null foreach (T v in vec) tmp += v; return (tmp / vec.Length) as T; //mask to the Generic type } Hope that helped Nassos

        "Success is the ability to go from one failure to another with no loss of enthusiasm." Winston Churchill "Quality means doing it right when no one is looking." Henry Ford

        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