A list of multiple data types?

I have two classes as such:

public class MachineLine
{
    public double X1;
    public double Y1;
    public double X2;
    public double Y2;
    public double Thickness;
}

public class MachineCircle
{
    public double CenterX;
    public double CenterY;
    public double Radius;
}

I want to make a List which can hold both of these, but I don't want my list to be able to hold any other data type. How can this be done?

Answers


The easiest way to do this is to declare an interface, and have both types implement it:

public interface IMachine { … }

public class MachineLine : IMachine
{
    public double X1;
    public double Y1;
    public double X2;
    public double Y2;
    public double Thickness;
}

public class MachineCircle : IMachine
{
    public double CenterX;
    public double CenterY;
    public double Radius;
}

Then you can do this:

List<IMachine> m = new List<IMachine>();

Need Your Help

Finding Duplicate Rows in MySQL (Composite Key)

mysql select key composite-key

I have a table with rows in the following format:

Chrome: Showing new window on top without giving it focus

google-chrome windows-7-x64

I'd like to open up a new chrome window (not tab) and have it take focus in its address bar immediately.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.