T
tshad
I am just trying to find out why you would use an interface with a list
class?
This is how I usually set up my list classes:
Racer.cs
***********************************************
public class Racer
{
private string name;
private string car;
public string Name { get { return name; } }
public string Car { get { return car; } }
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + ", " + car;
}
}
public class RacerCollection : List<Racer> { }
***********************************************
But I have seen others add an interface to their lists like so:
IRacer.cs
*******************************************
public interface IRacer
{
string Name { get; }
string Car { get; }
}
*******************************************
Racer.cs
************************************************
public class Racer : IRacer
{
private string name;
private string car;
public string Name { get { return name; } }
public string Car { get { return car; } }
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + ", " + car;
}
}
public class RacerCollection : List<IRacer> { }
************************************************
This works as well but I was curious as to why you would do it?
Seems like one more step that isn't necessary. There is no multiple
inheritance and multiple classes are not going to inherit it.
I did find (and can't remember where) that the List<IRacer> caused me a
problem somewhere when I tried to use it. And that was because it was
because it was a List of IRacer instead of a List of Racer.
Just curious.
Thanks,
tom
class?
This is how I usually set up my list classes:
Racer.cs
***********************************************
public class Racer
{
private string name;
private string car;
public string Name { get { return name; } }
public string Car { get { return car; } }
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + ", " + car;
}
}
public class RacerCollection : List<Racer> { }
***********************************************
But I have seen others add an interface to their lists like so:
IRacer.cs
*******************************************
public interface IRacer
{
string Name { get; }
string Car { get; }
}
*******************************************
Racer.cs
************************************************
public class Racer : IRacer
{
private string name;
private string car;
public string Name { get { return name; } }
public string Car { get { return car; } }
public Racer(string name, string car)
{
this.name = name;
this.car = car;
}
public override string ToString()
{
return name + ", " + car;
}
}
public class RacerCollection : List<IRacer> { }
************************************************
This works as well but I was curious as to why you would do it?
Seems like one more step that isn't necessary. There is no multiple
inheritance and multiple classes are not going to inherit it.
I did find (and can't remember where) that the List<IRacer> caused me a
problem somewhere when I tried to use it. And that was because it was
because it was a List of IRacer instead of a List of Racer.
Just curious.
Thanks,
tom