title: C# Graphics Draw Any Object
body: <p>I want to draw many different shapes on a Windows Form. The following code works only for rectangles.</p>
  
  <pre><code>// render list contains all shapes
  List&lt;Rectangle&gt; DrawList = new List&lt;Rectangle&gt;();
  
  // add example data
  DrawList.Add(new Rectangle(10, 30, 10, 40));
  DrawList.Add(new Rectangle(20, 10, 20, 10));
  DrawList.Add(new Rectangle(10, 20, 30, 20));
  
  // draw
  private void Form1_Paint(object sender, PaintEventArgs e)
  {
      Graphics g = e.Graphics;
      foreach (Rectangle Object in DrawList)
      {
          g.FillRectangle(new SolidBrush(Color.Black), Object);
      } 
  }
  </code></pre>
  
  <p>How can I improve the code to handle any type of shapes like rectangles, lines, curves, and so on?</p>
  
  <p>I think I will need a list which can contain objects of different types and a function to draw any object depending on its type of shape. But unfortunately I have no idea how to do that.</p>