开发者

C# - Implementing a private class with two public interfaces, then casting to both of them? Bad idea?

开发者 https://www.devze.com 2023-02-25 15:36 出处:网络
Is this considered a good idea? Type casting the same class to two different interfaces it implements. I think it\'s a good idea .. but am not sure.

Is this considered a good idea? Type casting the same class to two different interfaces it implements. I think it's a good idea .. but am not sure.

public interface Abc
{
    int xyz { get; }
}

public interface Xyz
{
    int abc { get; }
}

internal class MyClass : Abc, Xyz
{

    public int xyz
    {
        get
        {
            return 0;
        }
    }

    public int abc
    {
        get
        {
            return 1;
        }
    }
}

        var myclass = new MyClass();

        var abc = myclass as Abc;
        var xyz =开发者_开发知识库 myclass as Xyz;


If it implements the interface, you will not need to cast it.

Use the properties from the interface.


I think this is one of the reasons, why interfaces got introduced. When using interface, in the end, you don't care about which concrete class implements it, as long as it implements this interface correctly.

Also, you don't need to cast. The assignment of class implementing interface and variable of this interface is implicit.


Yes it is a good idea, especially if your classes are fulfilling different roles. Abc and Xyz may not be the best examples but something like ICanValidate, IHaveDefault, IHaveAnEngine, etc. may be better :)

Where such a design shines is when certain functionality is optional. Take the ICanValidate example: somewhere in your persistence pipeline you safe cast your entity to ICanValidate. If it returns null then you ignore it; however, if it returns the interface you can call entity.IsValid().

Just a note on casting. Some answers have stated that you don't need to cast when an interface is implemented. This is true for implicit implementation --- for explicit implimentation you will require casting. But I guess that would be rather obvious :)


There is no question about good or bad to type casting, For ex. List is implementing IList and IEnumerable. when iteration is required on list IEnumerable is used, and when list operation (like Add item ) need to be performed IList need to be used.

so it depends on functionality (from which interface) you need. Also you do not need CASTING when interface is implemented by class.


First of all. If you want others to use your code you should really follow the Design Guidelines for Developing Class Libraries. It looks like you are using naming guidelines used in Java.

The only reason to make something internal is to prevent others from creating the class. It's still possible though using reflection. Other than that, theres no reason. If you do not want others to extend your class, simply make it sealed. But you should have a really good reason for that too.

Finally there is nothing wrong with your approach looking from a usage perspective. No code should care how the interface implementation looks like or how protected it is. Each user should only care in that it has an instance to an interface implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号