Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i declare this in Interface and Class in C#.

C#
viewTrains(string source,string destination):List<ITrain>


I am making a Console Application, There is a requirement of This Method, But i am in Confusion how To Declare This Method in Interface and Inherit in Class. I Search a Lot Like "List with Type Interface" but i haven't find Anything. and also Please tell me, What is called in C#. Please Help Me. Thanks.

What I have tried:

I Tried:-
C#
interface ITrain
{
        int Id { get; set; }
        string Name { get; set; }
        string Source { get; set; }
        string Destination { get; set; }
}

public List<ITrain> viewTrains(string source, string destination)
{
   
}

is it Right ? if Right Then Please Provide Example of this.
Posted
Updated 17-Aug-16 19:08pm
Comments
PIEBALDconsult 18-Aug-16 0:00am    
Looks OK, but your intent isn't clear.
Maciej Los 18-Aug-16 1:29am    
What ViewTrains method should do? Don't get me wrong, i know you want to return the list of Trains, but what source and destination variables are there for, especially due of their type (of string)?

1 solution

Quote:
viewTrains(string source,string destination):List<itrain>
This doesn't "look right" in the sense it is not valid C# code. It looks like a method declaration, but it isn't.

A primary use of Interfaces is to enable having a Collection of different Types of object instances cast to the same Interface Type(s) they have inherited; I assume that's what this question is really about.

edit: for information on how to serialize a collection of different Class instances that share a common Interface as a collection of Interface "instances:" see this post: [^].

So, you need to implement some Classes that inherit from ITrain, and have their own unique Properties, or Fields, or Methods:
C#
public class RegularTrains : ITrain
{
   // implement all the ITrain declared Properties

   // unique to this Class
   public int YearsInService { get; set; }

    public RegularTrains(int id, string name, string source, string dest, int yrsservice)
    {
        // assign all the ITrain parameters to the correct variables
        
        // unique to this class
        YearsInService = yrsservice;
    }
}

public enum TechnologyType
{
    Maglev,
    InternalCombustion,
    Other
}

public class HighSpeedTrains : ITrain
{
    // implement all the ITrain declared Properties

    // unique to this Class
    public TechnologyType TechType { get; set; }

    public HighSpeedTrains(int id, string name, string source, string dest, TechnologyType techtype)
    {
         // assign all the ITrain parameters to the correct variables
          
         // unique to this class  
         TechType = techtype;
    }
}
Now the question is: where and how do you create and store a list of all the Trains. One way is like this:
C#
public static class AllTrains
{
    public static List<ITrain> Trains { set; get; }

    public static List<ITrain> ViewTrains(string source, string destination)
    {
        // use Linq here to compose a List of ITRains where the supplied parameters
        // 'source and 'destination match an ITRain
    }
}
So, here you have a static class that keeps a Collection of Trains cast to their ITrain form. Now how does the list get items added; one way is to use the inheriting classes constructors:
public RegularTrains(int id, string name, string source, string dest, int yrsservice)
{
    // assign all the ITrain parameters to the correct variables
    
    // unique to this class
    YearsInService = yrsservice;

    // add the current instance to the static List
    AllTrains.Trains.Add(this);
}
Then, you can do something like this:
RegularTrain rtrain = new RegularTrain(1,"r1","Topeka", "Chicago", 14);
HighSpeedTrain strain = new HighSpeedTrain(2, "h1", "Topeka", "Chicago", TechnologyType.Maglev);

var allTrains = AllTrains.ViewTrains("Topeka", "Chicago");
Note that adding the new instance of the RegularTrain Class to the static List of ITrain did not require casting the instance to Type ITrain: the conversion is implicit in the Type system.

When you cast an object to an Interface it inherits from, essentially you have created a "view" of the object: that view restricts you to seeing (accessing) only the Properties or Methods declared in the Interface: the object is still "there" with all its unique fields, properties, methods: it has not been copied.

Use IntelliSense in Visual Studio to examine the List of ITrain, and look at a specific item, and you can get an idea of how Reflection can look beyond the interface into the fundamental Type.
 
Share this answer
 
v7
Comments
Karthik_Mahalingam 18-Aug-16 2:39am    
5!
BillWoodruff 18-Aug-16 2:42am    
Thanks, Karthik
Richard MacCutchan 18-Aug-16 3:55am    
5 from me too, excellent description. I now understand C# interfaces.
BillWoodruff 18-Aug-16 6:04am    
Hi Richard, I find the idea I could teach you anything about any aspect of C# and .NET ... somewhat unbelievable :) Are you pulling my leg here ?

Do keep in mind that I focused on only one aspect of the use of Interfaces ... the one I thought appropriate to the OP's questions (and my estimation of where they might be in their study/practice).

If you have any suggestions or critique on what I've written here, I, and, I am sure, others, can only benefit from your deep knowledge.
Richard MacCutchan 18-Aug-16 6:31am    
Not at all, I am always trying to learn new things. And from what you have written above (and what I think I understand) there is a subtle difference between C# interfaces and some other languages. Definitely no leg pulling, implied or otherwise.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900