"c# list of different objects" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "c# list of different objects" answered properly. Developers are finding an appropriate answer about c# list of different objects related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like c# list of different objects. Enter your desired code related query in the search bar and get every piece of information about C# code related question on c# list of different objects.
c# list of different objects
Or is there a better/different way to do this?
If your objects descend from a common class, then you can store them in the same collection. In order to do anything useful with your objects without throwing away type safety, you'd need to implement the visitor pattern:
public interface EntityVisitor
{
void Visit(Arc arc);
void Visit(Line line);
}
public abstract class Entity
{
public abstract void Accept(EntityVisitor visitor);
}
public class Arc : Entity
{
protected double startx;
protected double starty;
protected double endx;
protected double endy;
protected double radius;
public override void Accept(EntityVisitor visitor)
{
visitor.Visit(this);
}
}
public class Line : Entity
{
protected double startx;
protected double starty;
protected double endx;
protected double endy;
protected double length;
public override void Accept(EntityVisitor visitor)
{
visitor.Visit(this);
}
}
Once that's in place, you create an instance of EntityVisitor whenever you need to do something useful with your list:
class EntityTypeCounter : EntityVisitor
{
public int TotalLines { get; private set; }
public int TotalArcs { get; private set; }
#region EntityVisitor Members
public void Visit(Arc arc) { TotalArcs++; }
public void Visit(Line line) { TotalLines++; }
#endregion
}
class Program
{
static void Main(string[] args)
{
Entity[] entities = new Entity[] { new Arc(), new Line(), new Arc(), new Arc(), new Line() };
EntityTypeCounter counter = entities.Aggregate(
new EntityTypeCounter(),
(acc, item) => { item.Accept(acc); return acc; });
Console.WriteLine("TotalLines: {0}", counter.TotalLines);
Console.WriteLine("TotalArcs: {0}", counter.TotalArcs);
}
}
And for what its worth, if your open to trying new languages, then F#'s tagged unions + pattern matching are a handy alternative to the visitor pattern.
All those coders who are working on the C# based application and are stuck on c# list of different objects can get a collection of related answers to their query. Programmers need to enter their query on c# list of different objects related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about c# list of different objects for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of c# list of different objects while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "c# list of different objects". Visit this developer's friendly online web community, CodeProZone, and get your queries like c# list of different objects resolved professionally and stay updated to the latest C# updates.