title: Passing a function to an Object at Instantiation in C#
body: <p>I am currently working on a project where I am trying to make an GUI for programming an I2C device. I have a bunch of register addresses and values in those registers both in hex strings like so:</p>
  <pre><code>[Address, Value][0x00, 0x01][0x01, 0xFF][0x02, 0xA0]... //not code, just abstract representation
  </code></pre>
  <p>Each register value represents something different. I have to convert each register value from a hex string to its associated human-understandable representation. My plan for this is to create a dictionary whose keys are the register addresses and whose values are a register object. The register object would contain information about the register like a description and a conversion function which takes in the hex string and outputs a converted value. The conversion function is different for every register since they represent different things. I want to create a generic register class and pass in each register's unique conversion function at instantiation. Passing this function is where I'm not sure if I'm making things more complicated than they need to be.</p>
  <p>My code currently looks like this:</p>
  <pre class="lang-cs prettyprint-override"><code>
      private class my_register
      {
          private string Description;
          {get;}
          
          //using delegate to be able to store function specified outside of class
          public delegate string convert_del(string reg_val);
          public convert_del conversion_func;
          
          //constructor uses a delegate to store the function passed at instantiation
          my_register(string desc, func&lt;string, string&gt; convert_func_input)
          this.Description = desc;
          this.conversion_func = new convert_del(convert_func_input) 
      }
      //Now I can create the object and pass in the function
      static void Main()
      {
          // lambda function is just a simple place holder to show that I can pass a function
          my_register first_reg = new my_register(&quot;Temp&quot;, reg_val =&gt; (Convert.ToInt32(reg_val, 16) 
                                                                      + 10).ToString())
          Console.WriteLine(first_reg.conversion_func(&quot;0x0A&quot;)) //output is 20
      }
  
  </code></pre>
  <p>This code works at least for the minimal testing I ran. This took me a long time to figure out, perhaps because I just didn't understand delegates well, but I am wondering if this is a convoluted way of going about this in C#. I come from a python background, though I'm not particularly skilled in that either. The way I would do this in python is as follows:</p>
  <pre class="lang-py prettyprint-override"><code>
      class my_register(object):
          def __init__(self, desc, conversion):
              self.Description = desc
              self.conversion = conversion
      
      def temp_convert(reg_val):
          return str(int(reg_val, 0) + 10)
      
      first_reg = my_register(&quot;Temp&quot;,  temp_convert)
      first_reg.conversion(10) #returns '20'
  
  </code></pre>
  <p>The pythonic way just seems way simpler, so I'm wondering if there a better more canonical way of achieving this function passing in C# or if there is a way to avoid passing the function all together?</p>