looking for exercises to practice interfaces on c#
i`m new at c# and i have learned about the interfaces subject so i am looking for a little project that i can do , that i will need to use interface , so i will understand this subject better
if some one have a pdf from scool or uni with exercises i`ll be happy to get it :)
Re: looking for exercises to practice interfaces on c#
Search bing or google for "C# interface tutorial".
Re: looking for exercises to practice interfaces on c#
In general if you are wanting to understand interfaces. I say just get in there spin up a simple console application and give it a try.
An interface is nothing more than a contract between it and its implementer. So basically the interface is saying "hey you implementer, you have to have this method, field, etc...I don't care what you do with it but you have to have it."
Code:
//interface
public interface IMyInterface
{
string MyMethod();
}
//implementer 1
public class MyInterfaceImplementer : IMyInterface
{
public string MyMethod()
{
return "My First String";
}
}
//implementer 2
public class MyInterfaceImplementer2 : IMyInterface
{
public string MyMethod()
{
return "My 2nd string";
}
}
//main - pseudo-code
static void Main()
{
IMyInterface iTest = null;
iTest = new MyInterfaceImplementer();
var value1 = iTest.MyMethod();
//value1 will be "My First String"
iTest = new MyInterfaceImplementer2()
var value2 = iTest.MyMethod();
//value 1 will be "My 2nd string"
}
so you can see that iTest is of type IMyInterface but since both classes implement IMyInterface we can use the same variable when declaring a new instance of each class. But they return different values when calling the same method.
hope this helps...
-zd
Re: looking for exercises to practice interfaces on c#
Write a plugin wrapper. :) Possibly one of the best scenarios where an interface is used.
Re: looking for exercises to practice interfaces on c#
To understand interfaces you first need to have at least some idea of the concept of type abstraction (generalisation), type derivation and specialization, and inheritance in object-oriented programming (both interface inheritance and implementation inheritance). Knowing a bit about composition doesn't hurt as well. Are you familiar with any of these things? If not, start by learning what classes and objects are, and how they interact together, and worry about interfaces later. Interfaces are all about making this interaction possible, maintainable, and flexible.
Re: looking for exercises to practice interfaces on c#
You should be able to develop a console based (or text style) game using interfaces as well, as long as you take on a good OO model for the design. You can write a strictly procedural game as well, so it's for the most part awareness of what you're doing with your objects that would make the difference. Inheritance from base classes, and interfaces for perhaps how each enemy interacts or behaves would be a good setup.