Un delegado no es más que un puntero a una función, hace referencia a un método.
La función en sí misma es un parámetro a suministrar
Es un tipo de dato referenciado, guarda la referencia a un método.
Se puede declarar con la palabra reservada delegate
public delegate void fillTextBox(string parameter1, string parameter2);
Cuando querramos utilizar el método fillTextBox, deberemos usarlo en métodos que tengan el mismo valor de retorno(en este caso void) y los mismos parámetros declarados(en este caso dos strings), sino no podrá ser llamado.
string textvalue;
public delegate void fillTextBox(int x);
public void fillMathNote(int examNote)
{
textvalue = "Your math note is" + examNote;
}
private void Button1_Click(object sender, EventArgs e)
{
//delegado
fillTextBox filltext = fillMathNote;
fillText(80);
}
Otras formas de declarar delegados
string textvalue;
public delegate void fillTextBox(int x);
public void fillMathNote(int examNote)
{
textvalue = "Your math note is" + examNote;
}
private void Button1_Click(object sender, EventArgs e)
{
//delegado
fillTextBox filltext = new fillTextBox(fillMathNote);
fillText(80);
}
string textvalue;
public delegate void fillTextBox(int x);
public void fillMathNote(int examNote)
{
textvalue = "Your math note is" + examNote;
}
private void Button1_Click(object sender, EventArgs e)
{
//delegado
fillTextBox filltext = fillMathNote;
fillText.invoke(80);
}
Un delegado puede apuntar a múltiples métodos
Estos reciben el nombre de Delegados Multicasting
Podemos añadir métodos con el operador de suma y restarlos con la resta
string textvalue;
public delegate void fillTextBox(int x);
public void fillMathNote(int examNote)
{
textvalue = "Your math note is" + examNote;
}
public void fillChemistryNote(int examNote)
{
textvalue = "Your Chemistry note is " + examNoteM;
}
public void showInTextBox()
{
textvalue += Enviorment.NewLine;
textvalue += Enviorment.NewLine;
textvalue += Enviorment.NewLine;
textBox1.Text = textvalue;
}
private void Button1_Click(object sender, EventArgs e)
{
fillTextBox filltext = fillMathNote;
filltext += fillChemistryNote;
fillText(80);
showInTextBox()
filltext -= fillMathNote;
filltext(60);
showInTextBox()
}