tags:

views:

30

answers:

3

In other words, will line 2 in the below code have any effect?

// let's assume that myForm has not previously been added  
myControlCollection.Add(myForm);
myControlCollection.Add(myForm); // line 2 
+2  A: 

No the second line won't have any effect. The collection won't add the same control instance twice.

Darin Dimitrov
+2  A: 

No effect will be there on execution of second line.

AAP
+2  A: 

I believe that executing Add twice in immediate succession will have no obvious effect. However, if there are intervening Add calls with other controls, it will - because Add updates the Z-order, sending the newly added control to the back. For example:

using System;
using System.Drawing;
using System.Windows.Forms;

class Test
{
    static void Main()
    {
        Form f = new Form();
        Button b1 = new Button
        {
            Location = new Point(50, 50),
            Size = new Size(40, 40),
            Text = "b1"
        };
        Button b2 = new Button
        {
            Location = new Point(70, 70),
            Size = new Size(40, 40),
            Text = "b2"
        };

        f.Controls.Add(b1);
        f.Controls.Add(b2);
//        f.Controls.Add(b1); 
        Application.Run(f);
    }
}

This shows b1 in front of b2 - but if you uncomment the second call to Add(b1), the order will be reversed.

Jon Skeet