I have a C# interface, and a concrete class that implements that interface. I now want to create another class that implements that interface. Simple enough.
However, most methods will be exactly the same in the classes, and only a couple of methods will actually change.
I don't want to duplicate all of the logic in my 2nd class that is contained in my first.
How do I create the 2nd class, and use the logic in my first class except for the extra stuff?
My interface is called IEventRepository, and my 1st class is called BaseEvents. I now want to create a new class called FooBarEvents.
My class definition for FooBarEvents is:
public class FooBarEvents : BaseEvents, IEventRepository
My intention was to then use the return base.Method() in each method that duplicates the code.
I'm assuming this isn't correct?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
The following code shows how to provide a common implementation of some interface methods with an abstract base class, and provide custom implementations for others.
public interface IEventRepository
{
void Method1();
void Method2();
}
public abstract class BaseEvents : IEventRepository
{
public void Method1()
{
Console.WriteLine("This is shared functionality");
}
public abstract void Method2();
}
public class Implementation1 : BaseEvents
{
override public void Method2()
{
Console.WriteLine("Impl1.Method2");
}
}
public class Implementation2 : BaseEvents
{
override public void Method2()
{
Console.WriteLine("Impl2.Method2");
}
}
public class Program
{
static void Main(string[] args)
{
var implementations = new List<IEventRepository> { new Implementation1(), new Implementation2() };
foreach (var i in implementations)
{
Console.WriteLine(i.GetType().Name);
Console.Write("\t");
i.Method1(); // writes 'This is shared functionality'
Console.Write("\t");
i.Method2(); // writes type specific message
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With