Вопрос по c# – Методы делегатов против общих методов

10

Error: User Rate Limit Exceeded

For Example :


With Delegate :

delegate void DelMethod(string str);

static void Method(string str)
{
    Debug.WriteLine(str);
}

Usage :

DelMethod dm = new DelMethod(Method);
dm(string);

And Without Delegate :

static void Method(string str)
{
    Debug.WriteLine(str);
}

Usage :

Method(string)

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

В чем причина этого ??

Ваш Ответ

4   ответа
1

delegate void DelMethod(string str = "hai");  
static void Method(string str = "bye")
{
    Debug.WriteLine(str);
}

DelMethod dm = Method;
dm();

//prints "hai"

Method();

//prints "bye"

3

// class where you know something
class A
{
   //delegate for answer
   public Func<bool> AskForSomething { get; set; }

   public void DoSomething()
   {
      //some code
      if(AskForSomething())
      {
          //do something
      }
      else
      {
          //do something else
      }
   }
}

class B
{
   public void Test()
   {
      A a = new A();
      a.AskForSomething = new Func<bool>(Answer);
      a.DoSomething();
   }

   private bool Answer()
   {
       return true;
   } 
}

Class Aclass BAnswerclass Bprivateclass A

2

private

For Example :

namespace DelegatesSample
{
    public delegate void privateCallbackDelegate(string param1, int param2);
    public class A
    {
        private void DelegatedMehod(string param1, int param2)
        {
            //Code for some action
        }

        private void CallBusinessClass()
        {
            privateCallBackDelegate del = new privateCallBackDelegate(DelegateMethod);

            B b = new B();
            b.InvokeBusinessMethod(del);
        }
    }

    public class B
    {        
        public void InvokeBusinessMethod(privateCallbackDelegate d)
        {
            //Do Some calculations and when finished, call the callback delegate
            d("Done", result); //or d.Invoke or d.BeginInvoke
        }

    }
}

:

Delegates are useful when:

A single method is being called. A class may want to have multiple implementations of the method specification. It is desirable to allow using a static method to implement the specification. An event-like design pattern is desired (for more information, see the Events Tutorial). The caller has no need to know or obtain the object that the method is defined on. The provider of the implementation wants to "hand out" the implementation of the specification to only a few select components. Easy composition is desired.
18

public Customer Find(string name)
{
    foreach (var customer in database.Customers)
       if (customer.Name == name)
          return customer;
}

public Customer FindByName(string name)
{
    foreach (var customer in database.Customers)
       if (customer.Name == name)
          return customer;
}

public Customer FindByAddress(string address)
{
    foreach (var customer in database.Customers)
       if (customer.Address == address)
          return customer;
}

public Customer Find(Predicate<Customer> p)
{
    foreach (var customer in database.Customers)
       if (p(customer))
          return customer;
}

var byName = Find(a => a.Name == "lorem");
var byAddress = Find(a => a.Address == "ipsum");
var byTelephone = Find(a => a.Telephone == "dolor");

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы