Ovviamene come prima cosa devi creare l'inerfaccia
Faccio un esempio : crea nel tuo progetto un file di nome IMyInterface.cs ( o dai un nome a piacere) e crea i metodi
codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for MyInterface
/// </summary>
public interface IMyInterface
{
string HelloWorld(string message);
int Somma(int x, int y);
}
Una volta fatto questo creare un nuovo file di nome ImplementInterface ( ( o dai un nome a piacere).
A questo punto ti trovi in questa condizione e cioè con una classe vuota :
codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class ImplementInterface
{
}
Adesso estendi la classe con la tua interfaccia in questo modo
codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class ImplementInterface : IMyInterface
{
}
Adesso ti basta evidenziare con il mous la parola IMyInterface e premendo il tasto destro
del mouse ti compare un menù. In alto dovresti trovare una voce "Implement Interface".
Clicca e magicamente vedrai i metodi da implementare nella tua classe. Ti troverai quindi in questa situazione :
codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ImplementInterface
/// </summary>
public class ImplementInterface : IMyInterface
{
public ImplementInterface()
{
}
public string HelloWorld(string message)
{
throw new NotImplementedException();
}
public int Somma(int x, int y)
{
throw new NotImplementedException();
}
}
Come puoi notare ora nella classe hai i metodi pronti per essere implementati
Un esempio di implementazione :
codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ImplementInterface
/// </summary>
public class ImplementInterface : IMyInterface
{
public ImplementInterface()
{
}
public string HelloWorld(string message)
{
return message;
}
public int Somma(int x, int y)
{
int somma = x + y;
return somma;
}
}