0

Iam very new to C#. I am learning more about delegates. When I run this code, I get the following error:

A field initializer cannot reference the nonstatic field at the line:

CalArepointer cpointer = CalculateArea;

Here is my program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace calculatearea
{
    class Program
    {
        delegate double CalArepointer(int r);    
        CalArepointer cpointer = CalculateArea;

        static void Main(string[] args)
        {    
            double area = cpointer.Invoke(20);    
            Console.ReadKey();   
        }

        double CalculateArea(int r)
        {
            return 3.14 * r * r;
        }
    }
}
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
demonslayer1
  • 741
  • 2
  • 16
  • 24
  • Possible duplicate of [A field initializer cannot reference the nonstatic field, method, or property](http://stackoverflow.com/questions/14439231/a-field-initializer-cannot-reference-the-nonstatic-field-method-or-property) – meJustAndrew Oct 15 '16 at 05:37
  • Please refer below link for more detail http://stackoverflow.com/questions/923343/what-does-a-field-initializer-cannot-reference-non-static-fields-mean-in-c – Sandip Oct 15 '16 at 05:52
  • Issue you are getting has nothing to do with `Delegates`, it is that `CalculateArea` method needs to be either static or need instance access when access from `static Main`, program is not able to access the `CalculateArea` method, which is same compilation error with or without Delegates. I have made it static. – Mrinal Kamboj Oct 15 '16 at 06:11

2 Answers2

1

You should initialize the value of cpointer inside the static context of Main method since there you are going to use it:

class Program
{
    delegate double CalArepointer(int r);    
    CalArepointer cpointer;

    static void Main(string[] args)
    {   
        cpointer = CalculateArea; 
        double area = cpointer.Invoke(20);    
        Console.ReadKey();   
    }

    double CalculateArea(int r)
    {
        return 3.14 * r * r;
    }
}
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • how come i cant initialize the value of cpointer outside of the main method – demonslayer1 Oct 15 '16 at 05:36
  • @user3289032 you can do it inside a constructor, or inside of a factory method, but you can't do this outside them. How should be the compiler able to know which section of code should initialize first? the `cpointer` or the `CalculateArea` method? – meJustAndrew Oct 15 '16 at 05:39
0

for the pure sake of initializing the value of cpointer outside of the main method, you want to use Static members

   class Program
    {
        delegate double CalArepointer(int r);

        static CalArepointer cpointer = CalculateArea;

        static double CalculateArea(int r)
        {
            return 3.14 * r * r;
        }


        static void Main(string[] args)
        {
            double area = cpointer.Invoke(20);
            Console.WriteLine(area);
            Console.ReadKey();
        }

    }
user3598756
  • 28,893
  • 4
  • 18
  • 28