Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've been developing software in C# for a while, but one major area that I don't use effectively enough is interfaces. In fact, I'm often confused on the various ways they can be used and when to use them. For example, I know methods can return interfaces, can take them as parameters, can be derived from them etc. This concept is a definite weakness for me

I was wondering if anyone knew of a source / tutorial that clearly and thoroughly explains interfaces in depth and the various ways they can be used?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
98 views
Welcome To Ask or Share your Answers For Others

1 Answer

Description

Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference.

For example:

public class FileLog : ILog
{
    public void Log(string text)
    {
        // write text to a file
    }
}

public class DatabaseLog : ILog
{
    public void Log(string text)
    {
        // write text to the database
    }
}

public interface ILog
{
    void Log(string text);
}

public class SomeOtherClass
{
    private ILog _logger;

    public SomeOtherClass(ILog logger)
    {
        // I don't know if logger is the FileLog or DatabaseLog
        // but I don't need to know either as long as its implementing ILog
        this._logger = logger;
        logger.Log("Hello World!");
    }    
}

You asked for tutorials.

Tutorials


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...