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

C#

 
AnswerRe: Trying to make a DataGridView field a color if a condition is met. Pin
Mathi Mani22-May-15 13:00
Mathi Mani22-May-15 13:00 
GeneralRe: Trying to make a DataGridView field a color if a condition is met. Pin
Norris Chappell23-May-15 11:11
Norris Chappell23-May-15 11:11 
Questionway around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 6:49
professionalBillWoodruff22-May-15 6:49 
AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Sascha Lefèvre22-May-15 7:04
professionalSascha Lefèvre22-May-15 7:04 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:12
professionalBillWoodruff22-May-15 21:12 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Sascha Lefèvre24-May-15 3:33
professionalSascha Lefèvre24-May-15 3:33 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff24-May-15 22:42
professionalBillWoodruff24-May-15 22:42 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Sascha Lefèvre25-May-15 4:21
professionalSascha Lefèvre25-May-15 4:21 
Hi Bill!

Explicit interface implementation doesn't stop me from accessing your Dictionary - I just have to treat an instance of the Example-class explicitly as an object of a type that implements IRequire:
C#
Example ex1 = new Example();
// casting ex1 to IRequire:
((IRequire)ex1).RequiredDictionary.Clear(); // probably not desired to be possible

// or assign the new instance to an IRequire-variable:
IRequire ex2 = new Example();
ex2.RequiredDictionary.Clear();

Explicit interface implementation is meant to solve the issue of potential member-name-collisions in case a class implements multiple interfaces. As an implication of this the members aren't visible on the implementing type but you can access them explicitly through the interface.

If you don't want the user of your class be able to perform arbitrary operations on your dictionary then you should either not include it in your interface at all (but instead methods like AddEntry and RemoveEntry; that they accomplish their task on a Dictionary would be an implementation detail of your class) or expose the Dictionary through an IReadOnlyDictionary[^]-interface (which would limit your class to .NET 4.5+) or a custom IReadOnlyDictionary-analog implementation to allow it to work with lower versions of .NET.

E.g.:
C#
public interface IRequire
{
    IReadOnlyDictionary<string, int> RequiredDictionary { get; }
    void AddEntry(string key, int i);
    void RemoveEntry(string key);
}

public class Example : IRequire
{
    public IReadOnlyDictionary<string, int> RequiredDictionary
    {
        get { return new ReadOnlyDictionary<string, int>(InternalDictionary); }
    }

    private Dictionary<string, int> InternalDictionary;

    public Example()
    {
        InternalDictionary = new Dictionary<string, int>();
    }

    public void AddEntry(string key, int i)
    {
        InternalDictionary.Add(key, i);
    }

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


BillWoodruff wrote:
I, for one, look forward to the upcoming provision in future versions of C# of method constraints: that's been mentioned in something I read recently (mabye Eric Lippert's blog ?), but I locate find the source right now.
I read about that too - I think Kent posted this recently. Yes, would be awesome I think Smile | :)

cheers, Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff25-May-15 7:54
professionalBillWoodruff25-May-15 7:54 
AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Richard Deeming22-May-15 7:11
mveRichard Deeming22-May-15 7:11 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:27
professionalBillWoodruff22-May-15 21:27 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff24-May-15 22:46
professionalBillWoodruff24-May-15 22:46 
AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Pete O'Hanlon22-May-15 8:06
mvePete O'Hanlon22-May-15 8:06 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:33
professionalBillWoodruff22-May-15 21:33 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Pete O'Hanlon22-May-15 21:47
mvePete O'Hanlon22-May-15 21:47 
AnswerRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
Alan N22-May-15 14:15
Alan N22-May-15 14:15 
GeneralRe: way around limitations of WinForms treatment of Public Proprties implementing an Interface ? Pin
BillWoodruff22-May-15 21:35
professionalBillWoodruff22-May-15 21:35 
QuestionHow to get the directory where DDL is installed Pin
Antonio Guedes22-May-15 6:14
Antonio Guedes22-May-15 6:14 
AnswerRe: How to get the directory where DDL is installed Pin
Eddy Vluggen22-May-15 6:56
professionalEddy Vluggen22-May-15 6:56 
GeneralRe: How to get the directory where DDL is installed Pin
Antonio Guedes22-May-15 10:06
Antonio Guedes22-May-15 10:06 
GeneralRe: How to get the directory where DDL is installed Pin
Richard Andrew x6422-May-15 12:28
professionalRichard Andrew x6422-May-15 12:28 
QuestionHow to implement Save As functionality in C# Pin
RajuPrasad8222-May-15 3:49
professionalRajuPrasad8222-May-15 3:49 
AnswerRe: How to implement Save As functionality in C# Pin
Dave Kreskowiak22-May-15 4:09
mveDave Kreskowiak22-May-15 4:09 
GeneralRe: How to implement Save As functionality in C# Pin
RajuPrasad8222-May-15 4:24
professionalRajuPrasad8222-May-15 4:24 
GeneralRe: How to implement Save As functionality in C# Pin
Dave Kreskowiak22-May-15 4:49
mveDave Kreskowiak22-May-15 4:49 

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.