Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    A Delegate of simple numeric functions.Lecture 5 - slide 33 : 45
Program 1
using System;

public class Application {

  public delegate double NumericFunction(double d);   

  public static void PrintTableOfFunction(NumericFunction f, 
                                          string fname, 
                                          double from, double to, 
                                          double step){
    double d;

    for(d = from; d <= to; d += step){
     Console.WriteLine("{0,10}({1,-4:F3}) = {2}", fname, d, f(d));
    }

    Console.WriteLine();
  }

  public static double Cubic(double d){  
    return d*d*d;
  }

  public static void Main(){
    PrintTableOfFunction(Math.Log, "log", 0.1, 5, 0.1);           
    PrintTableOfFunction(Math.Sin, "sin", 0.0, 2 * Math.PI, 0.1); 
    PrintTableOfFunction(Math.Abs, "abs", -1.0, 1.0, 0.1);

    PrintTableOfFunction(Cubic, "cubic", 1.0, 5.0, 0.5);

    // Equivalent to previous:
    PrintTableOfFunction(delegate (double d){return d*d*d;},  
                         "cubic", 1.0, 5.0, 0.5);
  }
}
 
 
 
 
This is a type named NumericFunction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
The method Cubic is of type NumericFunction
 
 
 
 
The static methods Math.Log, Math.Sin, and 
Math.Abs are also of type NumericFuncion
 
 
 
 
 
An nameless method in the type NumericFuncion