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 the
Interfaces are generally used to force implementing classes to obey a certain contract (meaning to have certain methods). Sometimes they are just used as markers i.e. they have no methods declared but enable some code generator to generate special code for the implementing class.
Having said this, interfaces are not really a "tool" in their own right, so they don't serve a particular functional purpose, but they can greatly simplify the design of your application and are used extensively in OO design patterns. A basic resource is this tutorial: http://www.csharp-station.com/Tutorial/CSharp/Lesson13
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.
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.