Why isn't this working ?
public interface IPoint
{
// not important
}
public interface IPointList
{
List<IPoint> Points { get; }
}
public abstract class Point : IPoint
{
// implemented IPoint
}
public abstract class PointList<TPointType> : IPointList
where TPointType: IPoint
{
public abstract List<TPointType> Points { get; } // <- doesn't compile
}
The TPointType obviously has to be an IPoint. Why this implementation is not allowed ?
regards,
Kate
As an answer to your comment, on how to get best of both worlds;
I was thinking of something like this, where you implement your interface GetPoints property explictily, create a GetPoints property that is 'more typed', and an protected abstract method that you can override in concrete implementations.
The 2 properties call the abstract implementation.
public abstract class PointList<T> : IPointList where T : IPoint
{
public IList<T> GetPoints
{
get
{
return GetPointsCore ();
}
}
IList<IPoint> IPointList.GetPoints
{
get
{
return GetPointsCore () as IList<IPoint>;
}
}
protected abstract IList<T> GetPointsCore();
}
The class PointList should implement the IPointList interface. Methods/properties cannot differ by return type only, which is what you're trying to do with the Points declaration in class PointList. If you implement
List<TPointType> Points { get; }
then logically you cannot implement
List<IPoint> Points { get; }
Because they would differ by return type only.
Related
I have an interface such as this one:
public interface ITestInterface
{
int a { get; set; }
void DoSomething();
}
Some of my classes are deriving from this interface:
public class OneClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine(this.a.ToString());
}
}
public class AnotherClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine((this.a * 2).ToString());
}
}
Since I now need a (large) common method on all classes derived from my interface, I was trying to provide an additional base class for that:
public class MyBaseClass
{
public void LargeCommonMethod()
{
Console.WriteLine((this.a * 3).ToString()); // no 'a' on base class
}
}
This clearly doesn't work because the base class would also need to implement my interface in order to know about that a field.
I am now asking myself what the best approach would be here:
make MyBaseClass inherit from ITestInterface?
set LargeCommonMethod() to protected and provide all internal data it uses via arguments? (There's actually a lot of these..)
skip the interface all along and replace it with an abstract base class?
...?
C# 8 provides a feature precisely for this scenario.
Your classes all implement an interface
You want to add a method to the interface
You don't want a breaking change to all of the existing classes. If you add a method to the interface all of the classes will break unless you find some way to add the method to all of them. (That includes modifying them all to inherit from a new base class.)
That feature is default interface methods.
You can add your method and a default implementation to the interface:
public interface ITestInterface
{
int a { get; set; }
void DoSomething();
void LargeCommonMethod()
{
Console.WriteLine((this.a * 3).ToString());
}
}
Your existing classes that implement the interface will not break. When cast as the interface, you'll be able to call the method which is defined in the interface. You can still modify any class to provide its own implementation, overriding the interface's default implementation.
For the method to be available the object must be cast as the interface - ITestInterface.
A lot of developers - including myself - found this to be an odd feature. But this is the scenario it's for.
Some documentation
The most common scenario is to safely add members to an interface already released and used by innumerable clients.
If you require a base implementation for a method then an interface is clearly not the way to go.
I would choose an abstract class instead and get rid of the interface. There is no need to complicate the design basically.
The Adapter pattern could fit your Use case, when you want to keep the ITestInterface consistent:
public interface ITestInterface
{
int a { get; set; }
void DoSomething();
}
public class TestInterfaceAdapter : ITestInterface
{
private readonly ITestInterface _testInterface;
public int a {
get => _testInterface.a;
set => _testInterface.a = value;
}
public TestInterfaceAdapter(ITestInterface testInterface)
{
_testInterface = testInterface;
}
public void DoSomething()
{
_testInterface.DoSomething();
}
public void LargeCommonMethod()
{
Console.WriteLine((this.a * 3).ToString());
}
}
public class OneClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine(this.a.ToString());
}
}
public class AnotherClass : ITestInterface
{
public int a { get; set; }
public void DoSomething()
{
Console.WriteLine((this.a * 2).ToString());
}
}
I may not be explaining this well, as I am not familiar with the vocabulary, but I have a base level interface, IMyStuff, that is inherited in a chain multiple times.
interface IOnceRemoved
{
IMyStuff MyStuff{get;set;}
}
interface ITwiceRemoved : IOnceRemoved
{
...
}
interface ITarget : ITwiceRemoved
{
...
}
public void MyMethod(ITarget target)
{
...
}
I have an object of the IMyStuff type. Is there a way to wrap this interface so MyMethod will accept it as an ITarget?
As Camilo already suggested, you need a wrapper like
class MyStuffAsTargetWrapper : ITarget
{
public MyStuffAsTargetWrapper(IMyStuff myStuff)
{
MyStuff = myStuff
}
public IMyStuff MyStuff{ get; set; }
}
and then call MyMethod(MyStuffAsTargetWrapper(myStuff)).
Define the following C# interface:
public interface IShape
{
int NumberOfLineSegments {get;}
int Area {get;}
}
Next, I want to define several rectangle classes: Trapezoid, square,etc. All of these classes differ in their Area() property, but NumberOfLineSegments() always returns 4. Therefore, I would like an 'interim' class or interface, called Rectangle (or IRectangle), that looks something like:
public Rectangle : IShape
{
public int NumberOfLineSegments{get{return 4;}}
}
I want Rectangle to implement only NumberOfLineSegment(), and leave it to its derived classes to implement the rest:
public Square : Rectangle
{
public int Area() {get{return length*height;}
}
However, since IShape is an interface, the Rectangle class must implement also Area(), which it knows not how to implement..
Thus I seem to be stuck, either with defining a 'dummy' Area() method for Rectangle, or not using inheritence at all.
Is there a way to circumvent this? I have read extensively through Richter's clr via c#, and in StackOverflow. Thanks in advance!
Rectangle class should be abstract and define Area() method as abstract.
public interface IShape
{
int NumberOfLineSegments {get;}
float Area{get;}
}
public abstract class RectangleBase : IShape
{
public int NumberOfLineSegments { get { return 4; } }
public abstract float Area { get; }
}
public sealed class Square : RectangleBase
{
public override fload Area() { get { return length*height; }
}
And if you need Rectangle instances:
public sealed class Rectangle : ReectangleBase
{
public int NumberOfLineSegments { get { return 4; } }
public float Area { get { throw new NotImplementedException(); } }
}
There are two options.
Make the implementation virtual and empty (or throw a NotImplementedException), so it does nothing by default until derived.
Make the base class abstract and create abstract signatures for the interface methods you want to force down the chain.
Number 2 is much more preferable, as it forces derived classes to implement the method, whereas in number 1 derived classes are not forced to override base virtual methods.
Abstract methods can successfully satisfy interface definitions as the compiler will know that abstract classes themselves cannot be instantiated, and any derived classes are forced to have the abstract method implemented.
That said, if there are interface members that do not make sense to a particular type, it is usually an indicator to break down your interfaces:
public interface IShape : ICalculateArea, IHaveLineSegments
{
}
public interface ICalculateArea
{
float Area { get; }
}
public interface IHaveLineSegments
{
int NumberOfLineSegments { get; }
}
class Rectangle : IHaveLineSegments
{
public int NumberOfLineSegments { get; private set; }
}
class Square : Rectangle, IShape
{
public float Area { get; private set; }
}
define the method as abstract.
public abstract float Area{get;}
Use an abstract class which implements the interface:
public abstract class Rectangle : IShape {
public int NumberOfLineSegments { get { return 4; } }
public abstract float Area { get; }
}
Your particular rectangle classes then simply inherit from the Rectangle abstract class.
This be ok? Forces implementation in derived classes.
public abstract class Rectangle : IShape
{
NumberOfLineSegments{get{return 4;}}
abstract float Area { get; }
}
Personally I prefer the #sll's solution (and the others which are essentially the same), but for the record there's one more way:
public interface IShape
{
int NumberOfLineSegments {get;}
float Area{get;}
}
public class Rectangle
{
public int NumberOfLineSegments { get { return 4; } }
}
public sealed class Square : Rectangle, IShape
{
public float Area() { get { return length*height; }
}
This way you can spare abstract class, with a cost of defining a rigid superclass (so you would be unable to have different superclass for the Square).
Note that this works despite of the fact that Rectangle does not implement IShape.
I have a design issue and am looking for the best design solution. I have added an example of the issue below.
public interface IVehicle<T>
{
int GetEngineSize();
}
public class Car : IVehicle<Car>
{
public int GetEngineSize()
{
throw new NotImplementedException();
}
public bool HasSpolier()
{
return true;
}
}
public class Bus : IVehicle<Bus>
{
public int GetEngineSize()
{
throw new NotImplementedException();
}
}
public abstract class BaseController<T>
{
public IVehicle<T> Repository { get; set; }
}
public abstract class CarController : BaseController<Car>
{
public CarController()
{
// How can I access the HasSpolier method from the IVehicle<T> without having to cast the Interface to concrete class Car
bool result = Repository.HasSpolier();
}
}
I'm not sure your generics are doing what you want here.
If instead of
IVehicle<T> Repository {get; set;}
You did
T Repository {get; set;}
You could make
public abstract class BaseController<T> where T : IVehicle
To ensure that they're of the IVehicle Interface
Then you'd have a typed repository and get access to your spoiler method.
You're doing IVehicle<Bus> but at least in the sample code, the T is never used in the interface. At this point the T is worthless.
Unless you implement the method in the interface, you can't access it without casting it to another class.
You'd have to cast your Repository to Car.
It would make using your interface pointless as the dependency on the implementation which you're trying to remove is re-introduced.
Also the type parameter on your interface isn't required, you don't use it anywhere else in the interface...
public interface IVehicle
{
int GetEngineSize();
}
In this post I talked about using a generic base class to enable me to create repository classes without duplicating loads of basic plumbing code.
Each Repository is accessed through an interface. In the code below, I will only show one of the methods for the sake of brevity:
Interface:
IQueryable<Suggestion> All { get; }
Generic base class
public IQueryable<T> All
{
get { return _unitOfWork.GetList<T>(); }
}
Concrete class (implements the interface and extends the generic base class)
public IQueryable<Suggestion> All
{
get { return _unitOfWork.GetList<Suggestion>(); }
}
I anticipated that I would be able to simply strip the method out of the concrete class, and the compiler would use the generic base class implementation instead and work out that was intended to satisfy the interface. But no!
When I strip the method out I get the old 'does not implement interface member' error.
If I can't do this, have my efforts to use a generic base class not been pointless? Or is there a way around this?
Can you make the interface itself generic then implement a typed version in your concrete class?
public interface IRepository<T>
{
List<T> All { get; }
}
public class Repository<T>
{
public List<T> All
{
get { return new List<T>(); }
}
}
public class SuggestionRepository : Repository<Suggestion>, IRepository<Suggestion>
{ }
I'd still suggest using the generic interface since it will save you from repeating yourself, but this works too.
public interface ISuggestionRepository
{
List<Suggestion> All { get; }
}
public class Repository<T>
{
public List<T> All
{
get { return new List<T>(); }
}
}
public class SuggestionRepository : Repository<Suggestion>, ISuggestionRepository
{ }
Use the virtual keyword and put your interface on your concrete implementation..
public interface IMyInterface<T>
{
IQueryable<T> All { get; }
}
public abstract class MyBaseClass<T> : IMyInterface<T>
{
public virtual IQueryable<T> All
{
get { return _unitOfWork.GetList<T>(); ; }
}
}
public class MyClass : MyBaseClass<Suggestion>, IMyInterface<Suggestion>
{
}