Why can't I implement the static function (static void fction(Stuff S)) inside of Main
If I cut and paste the function outside of main, the program compiles and works fine.
When the program runs I get the output: "Monkey1: chocolate"

using System;
using System.Collections.Generic;

namespace Empty
    public class Stuff
        public string monkey

    public class Empty

        static void Main(string[] args)

            Stuff S1 = new Stuff();

            S1.monkey = "Banana";


            static void fction(Stuff S) // THIS GIVES ME AN ERROR
            {                           //IF I PUT THIS FUNCTION OUTSIDE OF MAIN THE 
                                        // PROGRAM WORKS
                S.monkey ="CHocolate "; // WHAT IS GOING ON?

            Console.WriteLine("Monkey 1: " + S1.monkey);


    }//End of Empty class

Recommended Answers

All 4 Replies

You cannot define a function inside another function in C like languages.
Could be done in a language like Pascal as an example.

Even if you could define methods within methods, why would you expect Fnction to become a static method of the Stuff class? The definition is not inside the Stuff class - not even indirectly.

ddanbe: Thx for the info. I haven't programmed in a while so there are some rules I forgot.

sepp2k: So I put the fnction inside the Stuff Class and it worked.
It turns out I can create a class of type Stuff that contains a function that has a parameter of type Stuff. That seemed weird to me at first. Thank you for pointing that out.

In C# parlance we don't speak of functions, we speak of classes which contain methods(functions). The instance of a class(new) is called an object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.