I'm sure that there is a number of forums where this question could be asked, some of them maybe better suited than this forum. But my point is that in this thread on this forum I've already started to help, and there is definitely a number of other users on this forum that would be able to answer this question too.
The thing is, to be able to help the OP, we need some more description about the problem and what he wants to achieve. Sorry, but I can't see the point in trying another forum in this situation.
Anyway, here is a simple sample code showing how you could use an interface for the types that is to be added to the list. To use the methods or properties defined in the interface there is no need to cast the list.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Rectangles();
Circles();
BothRectanglesAndCircles();
}
private static void Rectangles()
{
var list = new List<IGeometrical>
{
new Rectangle(0, 0, 50, 50),
new Rectangle(10, 10, 40, 40)
};
DisplayArea(list);
}
private static void Circles()
{
var list = new List<IGeometrical>
{
new Circle(0, 0, 15),
new Circle(15, 15, 15)
};
DisplayArea(list);
}
private static void BothRectanglesAndCircles()
{
var list = new List<IGeometrical>
{
new Rectangle(20, 20, 30, 10),
new Circle(15, 15, 45)
};
DisplayArea(list);
}
private static void DisplayArea(List<IGeometrical> list)
{
foreach (var geometrical in list)
{
Console.WriteLine("{0}: {1}",
geometrical.Name,
geometrical.GetArea());
}
}
}
public interface IGeometrical
{
string Name { get; }
double GetArea();
}
public class Rectangle : IGeometrical
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Rectangle(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
public string Name
{
get { return "Rectangle"; }
}
public double GetArea()
{
return Width * Height;
}
}
public class Circle : IGeometrical
{
public int X { get; set; }
public int Y { get; set; }
public int Radius { get; set; }
public Circle(int x, int y, int radius)
{
X = x;
Y = y;
Radius = radius;
}
public string Name
{
get { return "Circle"; }
}
public double GetArea()
{
return Math.PI * Radius * Radius;
}
}
}