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

Edited by treasure2387

4 Years
Discussion Span
Last Post by ddanbe

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.

Edited by treasure2387


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.