none
Accessibility Rules for a class member RRS feed

  • Question

  • class Class1
        {
             delegate void PrintDouble(double x); 
    
            public static void Main(string[] argv)
            {
                PrintDouble doubleObj = (double x) =>
                                        {
                                            Console.WriteLine(x.ToString());           
                                        };
                double[] doubleArray=new double[10];
                for (int i = 0; i < doubleArray.Length; i++)
                {
                    doubleArray[i] = 100 + i+1;
                }
                CallAction(doubleObj,doubleArray);
                Console.Read();
    
            }
    
            public static void CallAction(PrintDouble action,double []doubleArray)
            {
                for (int i = 0; i < doubleArray.Length; i++)
                {
                  action(doubleArray[i]);   
                }
            }
    
        }
    Error:
    Error	1	Inconsistent accessibility: parameter type 'Delegate1.Class1.PrintDouble' is less accessible than method 'Delegate1.Class1.CallAction(Delegate1.Class1.PrintDouble, double[])'	E:\wipro\c#\Delegate\DelegateOne\Delegate1\Class1.cs	30	28	Delegate1
    
    I know making delegate public will solve the error but I want to know basic reason behind such accessibility rules? 
    Sunday, January 10, 2016 4:53 PM

Answers

  • >>I know making delegate public will solve the error but I want to know basic reason behind such accessibility rules?

    Since the PrintDouble delegate is part of the signature of the publicly exposed method, you won't be able to call the method if you don't have access to the PrintDouble delegate, i.e. the accessibility of the method and one of its parameter types are inconsistent. That's why the delagate also needs to be public.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Sunday, January 10, 2016 5:00 PM