Click here to Skip to main content
15,890,438 members
Home / Discussions / C#
   

C#

 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Alan N25-May-15 23:22
Alan N25-May-15 23:22 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Agent__00725-May-15 23:56
professionalAgent__00725-May-15 23:56 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Gyana_Ranjan Dash26-May-15 21:08
Gyana_Ranjan Dash26-May-15 21:08 
GeneralRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# Pin
Armugam Indrani28-May-15 22:48
professionalArmugam Indrani28-May-15 22:48 
AnswerRe: Difference Between Equality Operator ( ==) and .Equals() Method in C# PinPopular
harold aptroot26-May-15 1:02
harold aptroot26-May-15 1:02 
QuestionDataRelation problem Pin
Supratik De25-May-15 6:35
Supratik De25-May-15 6:35 
AnswerRe: DataRelation problem Pin
Richard Deeming27-May-15 1:19
mveRichard Deeming27-May-15 1:19 
QuestionInterface implementation strategies : four techniques; but: when (if) to use (which) one of them Pin
BillWoodruff25-May-15 4:06
professionalBillWoodruff25-May-15 4:06 
This is a continuation, in a way, of a recent thread I started here, and I would like to remind you, gentle reader, that this forum was (once) the C# language discussion forum.

If this (and the code) provoke any interesting discussion of how you choose to implement Interfaces ... keeping in mind the several roles Interfaces can play (contract, interchange-object with restricted scope, etc.), I'll be ... all ears Smile | :)

The code shown here demonstrates four "styles" of interface implementation: in each case a Class with a Dictionary is being created, and the access to that Dictionary is restricted; the only thing "exposed" of the Dictionary are methods to add, and remove, key/value pairs.

1. focus on "object:" explicit implementation of Dictionary (thanks to Richard Deeming for reminding me to consider explicit implementation).

2. focus on "behavior:" implicit implementation of methods (as suggested by Sascha LeFavre and Pete O'Hanlon).

3. using separate interfaces to "separate" exposed object(s) from exposed method(s): probably not a good example, what interests me is the issue of when one might "break-out" Interfaces.

4. using a Custom Generic Class that inherits from Dictionary in an Interface. Meant to be an "extreme" example.
C#
using System;
using System.Collections.Generic;

namespace InterfaceImplementations
{
    // example #1 expose a Dictionary : explicit interface implementation
    public interface IRequire1
    {
        Dictionary<string, int> RequiredDictionary { get; }
    }

    // implement Dictionary so it is not publicly exposed
    // using explicit interface implementation
    public class Example1 : IRequire1
    {
        private Dictionary<string, int> internalDictionary { set; get; }

        // explicit implementation of IRequire
        Dictionary<string, int> IRequire1.RequiredDictionary
        {
            get { return this.internalDictionary; }
        }

        public Example1()
        {
            internalDictionary = new Dictionary<string, int>();
        }

        public void AddEntry(string str, int i)
        {
            internalDictionary.Add(str, i);
        }

        public void RemoveEntry(string key)
        {
            internalDictionary.Remove(key);
        }
    }

    // example #2 : expose methods that operate on the (implicit) Dictionary
    public interface IRequire2
    {
        void AddEntry(string key, int value );
        void RemoveEntry(string key);
    }

    public class Example2 : IRequire2
    {
        private Dictionary<string, int> internalDictionary { set; get; }

        public Example2()
        {
            internalDictionary = new Dictionary<string, int>();
        }

        public void AddEntry(string key, int value)
        {
            internalDictionary.Add(key, value);
        }

        public void RemoveEntry(string key)
        {
            internalDictionary.Remove(key);
        }
    }

    // example #3 : use two interfaces to distinguish between the Dictionary object
    // and the methods that can operate on that object
    public interface IRequire3Dictionary
    {
        Dictionary<string, int> RequiredDictionary { get; }
    }

    public interface IRequire3Methods
    {
        void AddEntry(string key, int value);
        void RemoveEntry(string key);
    }

    public class Example3 : IRequire3Dictionary, IRequire3Methods
    {
        private Dictionary<string, int> internalDictionary { set; get; }

        public Example3()
        {
            internalDictionary = new Dictionary<string, int>();
        }

        // explicit implementation of IRequire3Dictionary
        Dictionary<string, int> IRequire3Dictionary.RequiredDictionary
        {
            get { return this.internalDictionary; }
        }

        // implicit implementation of Methods of IRequire3Methods
        public void AddEntry(string key, int value)
        {
            internalDictionary.Add(key, value);
        }

        public void RemoveEntry(string key)
        {
            internalDictionary.Remove(key);
        }
    }

    // example #4 use a generic custom Class that inherits from Dictionary
    public interface IRequire4<T1,T2>
    {
        CustomDictionary<T1, T2> CustomDictionary { get; }
    }

    public class CustomDictionary<T1, T2> : Dictionary<T1, T2>
    {
        public void AddEntry(T1 key, T2 value)
        {
            base.Add(key, value);
        }

        public void RemoveEntry(T1 key)
        {
            base.Remove(key);
        }
    }

    public class Example4 : IRequire4<string, int>
    {
        private CustomDictionary<string, int> internalDictionary { set; get; }

        // explicit implementation
        CustomDictionary<string, int> IRequire4<string, int>.CustomDictionary
        {
            get { return internalDictionary; }
        }

        public Example4()
        {
            internalDictionary = new CustomDictionary<string, int>();
        }

        public void AddEntry(string key, int value)
        {
            internalDictionary.Add(key, value);
        }

        public void RemoveEntry(string key)
        {
            internalDictionary.Remove(key);
        }
    }
}
And, yes, I do test code I post:
C#
// put this you know where
Example1 example1 = new Example1();

example1.AddEntry("hello", 100);

Example2 example2 = new Example2();

example2.AddEntry("goodbye", 200);

// example1.RemoveEntry("hello");

Example3 example3 = new Example3();

example3.AddEntry("ex 4", 999);

Example4 example4 = new Example4();

example4.AddEntry("ex 4", 2222);

«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.


modified 25-May-15 10:14am.

AnswerRe: Interface implementation strategies : four techniques; but: when (if) to use (which) one of them Pin
Richard Deeming27-May-15 1:29
mveRichard Deeming27-May-15 1:29 
QuestionToolstrip grey border Pin
Member 1171517124-May-15 6:52
Member 1171517124-May-15 6:52 
AnswerRe: Toolstrip grey border Pin
Ravi Bhavnani25-May-15 9:53
professionalRavi Bhavnani25-May-15 9:53 
QuestionReport From Date to Date Pin
abdo.kouta23-May-15 5:45
abdo.kouta23-May-15 5:45 
AnswerRe: Report From Date to Date Pin
OriginalGriff23-May-15 6:15
mveOriginalGriff23-May-15 6:15 
GeneralRe: Report From Date to Date Pin
abdo.kouta23-May-15 8:02
abdo.kouta23-May-15 8:02 
AnswerRe: Report From Date to Date Pin
Mycroft Holmes23-May-15 17:22
professionalMycroft Holmes23-May-15 17:22 
QuestionRe: Report From Date to Date Pin
abdo.kouta23-May-15 21:37
abdo.kouta23-May-15 21:37 
AnswerRe: Report From Date to Date Pin
Mycroft Holmes24-May-15 0:28
professionalMycroft Holmes24-May-15 0:28 
GeneralRe: Report From Date to Date Pin
abdo.kouta24-May-15 4:02
abdo.kouta24-May-15 4:02 
GeneralRe: Report From Date to Date Pin
Mycroft Holmes24-May-15 14:16
professionalMycroft Holmes24-May-15 14:16 
AnswerRe: Report From Date to Date Pin
jschell24-May-15 7:01
jschell24-May-15 7:01 
Questionasp.net with c# Pin
Member 1158609823-May-15 0:50
Member 1158609823-May-15 0:50 
AnswerRe: asp.net with c# Pin
Sascha Lefèvre23-May-15 1:18
professionalSascha Lefèvre23-May-15 1:18 
AnswerRe: asp.net with c# Pin
OriginalGriff23-May-15 2:05
mveOriginalGriff23-May-15 2:05 
QuestionEncypt a number to a 6 unique digits code Pin
Jassim Rahma22-May-15 13:04
Jassim Rahma22-May-15 13:04 
AnswerRe: Encypt a number to a 6 unique digits code Pin
Dave Kreskowiak22-May-15 14:38
mveDave Kreskowiak22-May-15 14:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.