title: Instantiating a new object of T inside of a generic function in C#
body: <p>I have two models that can both use the function below. I created an Interface that both of these models implement.</p>
  
  <p>In the function below I am looping through the passed in list and using the values in it to create a new version of the list. </p>
  
  <pre><code>public static IList&lt;T&gt; GenericFunction&lt;T&gt;(IList&lt;T&gt; objList) where T : IProduct
  {
      // In this function I am doing the following:
  
      IList&lt;T&gt; retVal = new List&lt;T&gt;();
  
      foreach (T obj in objList)
      {
          T newObj = (T)Activator.CreateInstance(typeof(T));
  
          newObj.Price = obj.BasePrice + obj.OldPrice;
          newObj.Description = obj.Name + " " + obj.Description;
  
          retVal.add(newObj);
      }
  
      return retVal;
  }
  </code></pre>
  
  <p><strong>The above code is working, but I have a couple of questions:</strong></p>
  
  <p>In order to have a function like the above that works with any model that implements IProduct, doing what I did is my only option right? At first I was thinking I could just simply create a standard function that takes <code>IList&lt;IProduct&gt;</code> as a param and returns <code>IList&lt;IProduct&gt;</code>. But then I ran into the problem of instantiating a new object which led me to creating a Generic Function.</p>
  
  <p>Additionally, can I use an object initializer on newObj to set its properties?</p>
  
  <p>After seeing this <a href="https://stackoverflow.com/questions/10708925/creating-generic-variables-from-a-type-how-or-use-activator-createinstance">StackOverflow post</a> I feel like I should be doing something different.</p>