title: Cannot add an instance of my class to the list<my class>
body: <p>I have a Powershell script that declares a class and then tries to add an instance of this class to a list:</p>
  
  <pre><code>Add-Type -TypeDefinition @"
  using System.Text.RegularExpressions;
  public class BuildWarning
  {
      public string Solution { get; private set; }
      public string Project { get; private set; }
      public string WarningMessage { get; private set; }
      public string WarningCode { get; private set; }
      public string Key { get; private set; }
      public bool IsNew { get; set; }
      private static readonly Regex warningMessageKeyRegex = new Regex(@"^(?&lt;before&gt;.*)\([0-9,]+\)(?&lt;after&gt;: warning .*)$");
      public BuildWarning(string solution, string project, string warningMessage, string warningCode)
      {
          Solution = solution;
          Project = project;
          WarningMessage = warningMessage;
          WarningCode = warningCode;
          var match = warningMessageKeyRegex.Match(WarningMessage);
          Key = Solution + "|" + Project + "|" + match.Groups["before"].Value + match.Groups["after"].Value;
      }
  }
  "@
  
  [System.Collections.Generic.List``1[BuildWarning]] $warnings = New-Object "System.Collections.Generic.List``1[BuildWarning]"
  
  [BuildWarning] $newWarning = New-Object BuildWarning("", "", "", "")
  
  $warnings += $newWarning
  </code></pre>
  
  <p>At the last line I get an error:</p>
  
  <pre>
  Cannot convert the "System.Object[]" value of type "System.Object[]" to type
  "BuildWarning".
  At C:\development\temp\BuildWarningReportGenerator.ps1:93 char:17
  +                 $warnings += $newWarning
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
      + FullyQualifiedErrorId : ConvertToFinalInvalidCastException
  </pre>
  
  <p>I cannot figure out what the problem is. The type checks show that types of both <code>$warnings</code> and <code>$newWarning</code> are correct. How to fix this error?</p>