Matthew Layton
Matthew Layton

Reputation: 42239

Implementing interfaces in C# .NET

Consider the following interface

public interface ICustomData
{
    String CustomData { set; get; }
}

According to MSDN documentation, interfaces members are automatically public.

Lets say I now want to implement my interface:

public class CustomDataHandler : ICustomData
{
}

This would break at compile time, telling me that I have not implemented "CustomData"

This on the otherhand would work:

public class CustomDataHandler : ICustomData
{
    public String CustomData { set; get; }
}

My question is: If the member of the interface is automatically public, why MUST I declare the implementation in the class? If the interface declaration is syntactically identical to the class declaration, why can the compiler not infer this automatically from my interface?

EDIT:

My reason for asking. Imagine a scenario where you are building data models, entities etc. I might code some interfaces to these models like so:

public interface IUserAccount
{
    Guid Identity { set; get; }
    String FirstName { set; get; }
    String LastName { set; get; }
    String EmailAddress { set; get; }
    String Password { set; get; }
}

public interface IUserDataEntry
{
    Guid DataIdentity { set; get; }
    String DataName { set; get; }
    String Data { set; get; }
}

It would be far simpler to construct the models like so:

public class UserAccount : IUserAccount
{
}

public class UserDataEntry : IUserDataEntry
{
}

public class Combined : IUserAccount, IUserDataEntry
{
}

Upvotes: 1

Views: 1435

Answers (6)

LightStriker
LightStriker

Reputation: 21004

You must explicitly implement it because... You are not limited to implementing it that way. You could use a field, or do something else in your method. An interface is only a method contract that guaranty that this method exist.

public class CustomDataHandler : ICustomData
{
    public String CustomData
    {
        get { return "None of your business!"; }
        set { } // Decide to do nothing
    }
}

The interface only guaranty this method will exist. Not what you're gonna do with it.

EDIT: As for your edit of your question, you probably seek to have a parent class instead of an interface if you want to implement the method only once for a collection of classes. However, while you can combine interface, you cannot combine parent classes. But, you can add interface at different point of a classes hierarchy.

Upvotes: 0

Cristian Lupascu
Cristian Lupascu

Reputation: 40526

Here's an example where the CustomData property is private in a derived class:

public class CustomDataHandler : ICustomData
{
    private string CustomData { set; get; }

    string ICustomData.CustomData { get; set; }
}

But this code compiles, because there is also an explicit implementation of the property.

So, the public modifier is not redundant in this case.

Upvotes: 0

Trevor Pilley
Trevor Pilley

Reputation: 16393

The interface declaration is only specifying the behaviour which the interface defines. In your case, this is a property called CustomData which has a get and set (it is a read/write property) which is of type string.

The class which implements the interface needs to do exactly that - to specify the implementation.

Now in your case, you are using auto implemented properties { get; set; } which looks the same as the interface declaration, however you could also have a backing field and behaviour in your get or set methods.

Upvotes: 0

Will Vousden
Will Vousden

Reputation: 33358

They may be syntactically identical, but they mean different things (i.e. they are not semantically identical).

In the interface, the syntax means that an implementing class must expose such a property, with get and set accessors implemented as it sees fit (either explicitly or implicitly). An interface merely defines the outward behaviour that a class must provide; it does not provide any implementation of that behaviour.

In the class, the syntax is an "auto-property", an implementation of the property defined by the interface, and the get and set accessors are implicitly converted into full implementations with a backing field. It looks something like this when it's compiled:

public class CustomDataHandler : ICustomData
{
    private string customData;

    public string CustomData
    {
        get
        {
            return customData;
        }
        set
        {
            customData = value;
        }
    }
}

Upvotes: 2

Justin Harvey
Justin Harvey

Reputation: 14672

An interface is not there to provide an implementation, it is there to define a contract. This then allows for different implementations to be built which implement it.

Upvotes: 3

Rich O'Kelly
Rich O'Kelly

Reputation: 41757

You are implicitly implementing the interface. In this instance the method signatures of the class must match those of the interface (including accessibility). Ensuring that the methods are marked as public ensures that there are no surprises when looking at the class, for instance:

public class CustomDataHandler : ICustomData
{
  String CustomData {get; set}
  String PrivateCustomData {get;set;}
}

Even though both properties are declared the same, the CustomData property would be public by virtue of it being declared on the interface even though the declaration looks identical to that of PrivateCustomData. This would be inconsistent and lead to harder to maintain code.

If you do not wish to set the access modifier, you could explicitly implement the interface:

public class CustomDataHandler : ICustomData
{
    String ICustomData.CustomData { set; get; }
}

Upvotes: 1

Related Questions