Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.


Wednesday, September 5, 2018

What is Diamond Problem in C#

In this blog, we will learn what is Diamond problem in c# and We will learn Diamond problem in c# with a simple example.

Diamond Problem in C#

Prerequisites:

Why c# does not supports multiple inheritances?

One of the main reason behind this is the “diamond problem.

What is the Diamond Problem:


The "diamond problem" is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which class of the method does D inherit: that of B, or that of C? So this is an ambiguity problem in multiple inheritances in c#. So that c# does not support multiple inheritances. It also called an ambiguity problem in c#.


diamond problem in c#

Example 1: 

This is a simple example to understand the concepts of the diamond problem in c#.

First of all, we will check when does the diamond problem will arise with programmatically.
We will be creating class A, class B, class C, class D having Print() method in the base class. The class B and C inherits from class A. and class D inherit from class B and Class C as follow.


using System;

namespace CSharpConsoleApp.DiamondProblemExample
{
   public class A
    {
        public virtual void Print()
        {
            Console.WriteLine("Print method of class A.");
        }
    }

    public class BA
    {
        public override void Print()
        {
            Console.WriteLine("Print method of class B");
        }
    }

    public class CA
    {
        public override void Print()
        {
            Console.WriteLine("Print method of class C");
        }
    }

    // Error: D class can not have multiple base classes.
    public class DC, B
    {
        
    }

    class DiamondProblemExample
    {
        static void Main(string[] args)
        {
            D obj = new D();
            obj.Print();
        }
    }
}


How can we resolve the diamond problem in c#?

We can resolve the diamond problem by using Interface in c#.


Example 2:
using System;

namespace CSharpConsoleApp.DiamondProblemExample
{
    interface IA
    {
        void PrintIA();
    }
    interface IB
    {
        void PrintIB();
    }
    interface IC
    {
        void PrintIC();
    }
   public class A : IA
    {
        public void PrintIA()
        {
            Console.WriteLine("PrintIA method from class A.");
        }
    }

    public class B:IB
    {
        public void PrintIB()
        {
            Console.WriteLine("PrintIB method from class B.");
        }
    }

    public class C:IC
    {
        public void PrintIC()
        {
            Console.WriteLine("PrintIC method from class C.");
        }
    }
    
    public class D: IA, IB,IC
    {
        public void PrintIA()
        {
            Console.WriteLine("PrintIA method from class D.");
        }
        public void PrintIB()
        {
            Console.WriteLine("PrintIB method from class D.");
        }

        public void PrintIC()
        {
            Console.WriteLine("PrintIC method from class D.");
        }
    }

    class DiamondProblemExample
    {
        static void Main(string[] args)
        {
            D obj = new D();
            obj.PrintIA();
            obj.PrintIB();
            obj.PrintIC();
            Console.ReadLine();
        }
    }
}



OutPut:


Diamond Problem in C#

Real Time Diamond Problem in C# Example: 


diamond problem in c#


Real Time Diamond Problem in C# Solution: 



diamond problem in c#


A real-time example of resolving a diamond problem by using Interface in c#. 

using System;

namespace CSharpConApp.DiamondProblem1
{
    interface IMother
    {
        void Loan();
    }
    interface IFather
    {
        void Loan();
    }
    public class Mother:IMother
    {
        public void Loan()
        {
            Console.WriteLine("Loan taken by mother.");
        }
    }
    public class Father:IFather
    {
        public void Loan()
        {
            Console.WriteLine("Loan taken by father");
        }
    }
    public class Child:IMother,IFather
    {
        void IMother.Loan()
        {            
            Console.WriteLine("Mother's loan paid by child");
        }

        void IFather.Loan()
        {
            Console.WriteLine("Father's loan paid by child");
        }
    }

   public class DiamondProblem1
    {
        static void Main(string[] args)
        {
            Child child = new Child();
            ((IMother)child).Loan();
            ((IFather)child).Loan();

            Console.ReadLine();
        }
    }
}



Output:
Diamond Problem in C#

To understand the concepts of Implicit and Explicit interface visit the following link.


I hope you understood the concept of the diamond problem in C#.
Share:

2 comments:

  1. Hi,

    The direction of the arrows in either "two classes B and C inherit from A" or "D inherits from both B and C" is switched they should in both cases either start from parent and go to the child or the opposite, depending on the pattern you choose for the pictures (as it does not seem like an UML diagram).

    Thanks


    Thanks

    ReplyDelete
    Replies
    1. Thanks Paulo, The given diagram is correct in that case because first i am trying to explain when the diamond problem is occurred. the "D inherits from both B and C" if we are trying to inherits both the same time then it does not allow and That is the diamond problem diagram...... Thanks for suggestion.
      Please let me know in case of any concerns.

      Delete

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave