Get Datatype from the List of Different Datatype c#

Indudhar Gowda 426 Reputation points
2021-11-19T09:01:12.84+00:00

I need to get Datatype from the List of Different Datatype c#

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;
}


foreach (IMachineSomething sth in m)
{
    if (sth is MachineLine)
    {
        var machineLine = (MachineLine)sth;
        machineLine.DoThis();
    }
    else if (sth is MachineCircle)
    {
        var machineCircle = (MachineCircle)sth;
        machineCircle.DoThat();
    }
    else …
}

Requirement : I dont want to loop into each item, but get datatype from the List of different datatype.

Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2021-11-19T09:39:19.73+00:00

    If you want to perform some specific actions depending on type, then consider this approach:

    public interface IMachine 
    {
        void DoSometing1( );
    }
    
    public class MachineLine : IMachine
    {
        public double X1;
        public double Y1;
        public double X2;
        public double Y2;
        public double Thickness;
    
        public void DoSometing1( )
        {
            // . . .
        }
    }
    
    public class MachineCircle : IMachine
    {
        public double CenterX;
        public double CenterY;
        public double Radius;
    
        public void DoSometing1( )
        {
            // . . .
        }
    }
    
    . . .
    
    foreach( IMachine sth in m)
    {
        sth.DoSomething1( );
    }
    

  2. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-11-19T09:57:55.77+00:00

    Hi,
    try this console demo:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp2
    {
      class Program
      {
        static void Main(string[] args)
        {
          List<object> m = new List<object>();
    
          m.Add(new MachineLine());
          m.Add(new MachineCircle());
          m.Add(new MachineLine());
          m.Add(new MachineCircle());
    
          foreach (object sth in m.Where((o) => o is MachineLine))
          {
            var machineLine = (MachineLine)sth;
            machineLine.DoThis();
          }
    
          foreach (object sth in m.Where((o) => o is MachineCircle))
          {
            var machineCircle = (MachineCircle)sth;
            machineCircle.DoThat();
          }
    
        }
    
        public interface IMachine
        {
          public void DoThis();
          public void DoThat();
        }
    
        public class MachineLine : IMachine
        {
          public double X1;
          public double Y1;
          public double X2;
          public double Y2;
          public double Thickness;
          public void DoThis() => Console.WriteLine("MachineLine");
          public void DoThat() => throw new NotImplementedException();
    
        }
    
        public class MachineCircle : IMachine
        {
          public double CenterX;
          public double CenterY;
          public double Radius;
          public void DoThis() => throw new NotImplementedException();
          public void DoThat() => Console.WriteLine("MachineCircle");
        }
      }
    }