3

It doesn't matter what modifier I put on the front of the function(I've tried with public, private and even protected), I always receive an error, the same error. The code is clean only after I delete the modifier and I left the function "Array()" without one. Can someone look at my code and explain to me what is happening please, I am new to c#, and also new to asking help, so please excuse every mistake I've made so far.

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {

            public void Array()//nu se pune in interiorul functiei void Main (), deoarece va forma nesting, si ne va da eroare la compilare.
            {
                int[] intArray;
                intArray = new int[3];//all values will be 3


                var doubleArray = new[] { 34.23, 10.2, 23.2 };

                //var arrayElement = doubleArray[0];
                //doubleArray[1] = 5.55;

                for (var i = 0; i < intArray.Length; i++)
                {
                    Console.WriteLine(intArray[i]);
                }
            }

        }



    }
}

I've posted the code and the image of it down below.

In this image you can see the code

Corina
  • 137
  • 1
  • 1
  • 12
  • 12
    Please don't post images of code, add the actual code to your question. – DavidG Mar 13 '19 at 16:02
  • 1
    @Zer0 No it doesn't at all. – DavidG Mar 13 '19 at 16:02
  • @DavidG I wish I could do it , but I don't know how. When I wanted to paste the code, it only allowed html , css and javascript languages. I tried to change the language and it required me the URL adress of the place of the code, but I don't have my code on a site and I don't know if I can put it on one. – Corina Mar 13 '19 at 16:06
  • There is nothing to stop you pasting the code directly into the window, then make sure you press the "Code sample" button to format it. – DavidG Mar 13 '19 at 16:07
  • See [here](https://stackoverflow.com/help/formatting) on formatting posts. – Zer0 Mar 13 '19 at 16:08
  • @Zer0 thank you very much! – Corina Mar 13 '19 at 16:11

4 Answers4

9

You have a nested function, in C# these are called local functions and don't have scope. so you need to remove the access modifier, for example:

public static void PrintHelloWorld()
{
    string GetName()
    {
        return "world";
    }


    Console.WriteLine($"Hello {GetName()}");
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
5

You are creating a nested method (also called local function). Nested methods may not have access modifiers. They are only accessible from within this method.

For reference: https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/local-functions

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
0

You are putting your private function inside a static function. Remove the private.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
-2

Move the function out of the Main function. Also you should mark it as static and then use it as you intend. This in case you don't want a nested method as it is already answered.