It's not multiple inheritance, but something that comes to mind is Extension methods public interface IModifiable { DateTime ModifiedDate {get; set;} } public static class ModifiableExtensions { public bool ModifiedToday(this IModifiable m) { return DateTime.Now. AddDays(-1). CompareTo(m.
ModifiedDate) >= 0; } public bool ModifiedInLastWeek(this IModifiable m) { return DateTime.Now. AddDays(-7). CompareTo(m.
ModifiedDate) >= 0; } } That gives the "feel" of helper methods that are baked into the type, but they happen to be declared elsewhere. Take this class: public class MyModifiable :IModifiable { public ModifiedDate {get; set;} } And you can do this: MyModifiable m = new MyModifiable; m. ModifiedDate = DateTime.
Now; bool isToday = m.ModifiedToday().
It's not multiple inheritance, but something that comes to mind is Extension methods. Public interface IModifiable { DateTime ModifiedDate {get; set;} } public static class ModifiableExtensions { public bool ModifiedToday(this IModifiable m) { return DateTime.Now. AddDays(-1).
CompareTo(m. ModifiedDate) >= 0; } public bool ModifiedInLastWeek(this IModifiable m) { return DateTime.Now. AddDays(-7).
CompareTo(m. ModifiedDate) >= 0; } } That gives the "feel" of helper methods that are baked into the type, but they happen to be declared elsewhere. Take this class: public class MyModifiable :IModifiable { public ModifiedDate {get; set;} } And you can do this: MyModifiable m = new MyModifiable; m.
ModifiedDate = DateTime. Now; bool isToday = m.ModifiedToday().
1 -1: See my comments below under Scrum Master's post. – Tony Casale Mar 4 at 20:43.
No. C# doesn't have a mechanism to implement multiple inheritance this way. When it comes to interfaces, this is possible because when you define multiple interfaces you also need to implement them all.
Consider a different design, possibly using composition in order to reuse the classes you want to use for multiple inheritance.
Yes there are methods, several, actually. A few thoughts: Use an empty interface for Deletable, Modifiable etc (called tagging interfaces), then create extension methods for them. This is not as expandable as multiple inheritance, but it gets a long way.
Use genericity, possibly with the same tagging interfaces to create dependencies. This way you can have a base class with all methods for both Modifiable and Deletable, including abstract methods and override implementation in derived classes Use aspect oriented programming to get to the same results Almost the same, but do it yourself with Castle or similar library, possibly with the help of attributes. Obviously, none of the above has all the advantages of multiple inheritance.
If you want multiple inheritance in . NET, you can use C++. NET or Eiffel.NET.
I forget the design pattern name, but there's a pattern to implement multiple interfaces by wrapping the method/property calls around interface implementations of members who are of that same interface: interface IDrivable { void Drive(); } interface IFlyable { void Fly(); } class Car : IDrivable { public void Drive() { /* Implementation */ } } class Plane : IFlyable { public void Fly() { /* Implementation */ } } class MyClass : IDrivable, IFlyable { private IDrivable _car = new Car(); private IFlyable _plane = new Plane(); public void Drive() { _car.Drive(); } public void Fly() { _plane.Fly(); } }.
Sorry, multiple inheritance is not possible in C# and that's a bummer for you. Your choices are: Either to chain your base class inheritance a la MyClass : MyBaseClass : EvenBasierClass Or inherit from multiple interfaces. And implement all methods of all interfaces.It's not pretty, but you can also control property accessibility or return values inside your classes by checking the instance type.
Modifiable & Deletable IMO should be interfaces, not base classes. A base class defines what a type is, whereas a interface describes what a type does. As far as implementing the code, you can always use extension methods: public interface IModifiable { public DateTime ModifiedDate {get; set;} } public interface IDeletable { public DateTime DeletionDate {get; set;} } public static class SomeExtentions { public static bool IsModifiedToday(this IModifiable modifiable) { return DateTime.Now.
AddDays(-1). CompareTo(modifiable. ModifiedDate) >= 0; } public static bool IsModifiedInLastWeek(this IModifiable modifiable) { return DateTime.Now.
AddDays(-7). CompareTo(modifiable. ModifiedDate) >= 0; } public static bool IsDeleted(this IDeletable deletable) { return deletable.
DeletionDate! = default(DateTime); } }.
If only I had thought of that... – RQDQ Mar 4 at 20:34 @rqdq I see that you posted almost exact code solution, however the first line of my answer is the reason I didn't delete it. – The Scrum Meister Mar 4 at 20:35 I agree with the Interfaces comment. The extension methods are subjective, and I wouldn't use them as you have the ability to modify the class.
Extension methods are best when you can't modify the class directly. – Ritch Melton Mar 4 at 20:37 3 Extension methods break encapsulation, plus you've removed the calculated properties from the object itself, thus preventing subclasses from overriding their behavior. – Tony Casale Mar 4 at 20:41.
I would probably use delegation to achieve this. Create Modifiable and Deletable as interfaces, then create implementations of those. Give the Something class instances of these implementations.
Here's an example for Deletable: public interface Deletable { DateTime DeletionDate{get;set;} bool Deleted{get;} } public class DeletableImpl : Deletable { public DateTime DeletionDate{get; set;} public bool Deleted{get {return DeletionDate! = default(DateTime);}} } // Do the same thing with Modifiable and ModifiableImpl public class Something : Deletable, Modifiable { private Deletable _deletable = new DeletableImpl(); private Modifiable _modifiable = new ModifiableImpl(); public DateTime DeletionDate { get{return _deletable. DeletionDate;} set{_deletable.
DeletionDate = value;} } public bool Deleted{get{return _deletable. Deleted;}} public DateTime ModifiedDate { // and so on as above }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.