Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trying To Understand Delegates
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Trying To Understand Delegates
Divers
Thread ID:
01278832
Message ID:
01278832
Vues:
61
I need help understanding delegates. I have read a number of articles, but it all seems like
a lot of work to go through just to call a function.

See the lines markes in Main:
using System;
using System.Collections.Generic;
using System.Text;

public delegate string mydel(int number);

class Program
{
    static void Main(string[] args)
    {
        MyClass obj1 = new MyClass(10);
        MyClass obj2 = new MyClass(20);

        mydel del1 = obj1.myfunc;   // I don't understand these 2 lines
        mydel del2 = obj2.myfunc;

        Console.WriteLine(del1(50));
        Console.WriteLine(del2(20));
    }
}

public class MyClass
{
    int x;
    public MyClass(int x1)
    {
        x = x1;
    }
    public string myfunc(int n)
    {
        return (n * x).ToString();
    }
}
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform