'Can there be stand alone functions in C# without a Class?

In C/C++, I have a bunch of functions that I call from main(), and I want to rewrite this in C#. Can I have stand alone functions(methods) or do I have to put them in another class? I know I can have methods within the same class, but I want to have a file for each function/method.

Like this works:

using System.IO;
using System;


class Program
{
    static void Main()
    {
House balls = new House();
balls.said();

    }
}
    public class House
    {
        public void said()
        {
            Console.Write("fatty");
            Console.ReadLine();

        }
    }

But then I have to create an instance of House and call said(), when in C I can just call said().



Solution 1:[1]

For reference, I want to add the using static addition of C# 6 here.

You can now use methods of a static class without having to type the name of that class over-and-over again. An example matching the question would be:

House.cs

public static class House
{
    public static void Said()
    {
        Console.Write("fatty");
        Console.ReadLine();
    }
}

Program.cs

using static House;

class Program
{
    static void Main()
    {
        Said();
    }
}

Solution 2:[2]

No. Make them static and put them in a static utility class if they indeed don't fit within any of your existing classes.

Solution 3:[3]

There is no concept of standalone functions in C#. Everything is an object.

You can create static methods on some utility class, and call those without creating an instance of a class eg

class Program
{
    static void Main()
    {
    House.said();
    }
}

public class House
    {
        public static void said()
        {
            Console.Write("fatty");
            Console.ReadLine();

        }
    }

Solution 4:[4]

If using C# 9 it is now kinda possible, thanks to the top-level statements feature.

In your executable project, the following syntax is now allowed:

using SomeNamespace;

// The following statements are seemingly defined without even a method,
// but will be placed inside a "Main" static method in a "$Program" static class
SayHello();
var classFromSomeNamespace = new SomeClass(); // from SomeNamespace
classFromSomeNamespace.SomeMethod();

// This function is seemingly defined without a class,
// but on compile time it will end up inside a "$Program" static class
void SayHello()
{
    Console.WriteLine("Hello!");
}

// Here the "traditional" syntax may start
namespace SomeNamespace
{
    public class SomeClass
    {
        public void SomeMethod()
        {
            Console.WriteLine("SomeMethod called");
        }
    }
}

It should be noted, that the above syntax is valid only for a single file in a project, and the compiler actually still wraps this all inside a $Program static class with static methods. This feature was introduced specifically to avoid boilerplate code for the program entry point, and make it possible to easily write "scripts" in C#, while retaining the full .NET capabilities.

Solution 5:[5]

You have to put them in a class, but the class can be static as others mentioned. If you REALLY want to have a separate file for each method, you can mark the class as partial to get the following:

Program.cs
----------
class Program
{
    static void Main()
    {
        House.said();
        House.saidAgain();
    }
}

House-said.cs
-------------
public static partial class House
{
    public static void said()
    {
        Console.Write("fatty");
        Console.ReadLine();
    }
}

House-saidAgain.cs
------------------
public static partial class House
{
    public static void saidAgain()
    {
        Console.Write("fattyAgain");
        Console.ReadLine();
    }
}

I wouldn't recommend separating each one out, however. Partial classes are mostly used so that designer-generated code won't overwrite any custom code in the same class. Otherwise you can easily end up with hundreds of files and no easy way to move from one method to another. If you think you need a partial class because the number of methods is getting unmaintainable, then you probably need to separate the logic into another class instead.

Solution 6:[6]

Although the concept of stand-alone functions exists in .NET, C# doesn't allow you to specify such functions. You need to stick them inside a static Utils class or similar.

Solution 7:[7]

If you declare your method as static (that is: public static void said()) then you can just call it with House.said(), which is as close as you'll get in C#.

Solution 8:[8]

You could add all your methods to the Program class, but this would quickly become an unmaintainable mess, commonly referred to as the God Class or Ball of Mud anti-pattern.

Maintaining a single file for each function would similarly become a huge mess. The questions "Where do I put my methods" and "What classes should I create" are answered by Design Patterns. Classes aggregate behavior (functions) and should do one thing (Single Reponsibility.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 huysentruitw
Solution 2 Donnie
Solution 3 Winston Smith
Solution 4 Evengard
Solution 5 Nelson Rothermel
Solution 6 thecoop
Solution 7 Dan Puzey
Solution 8 Dave Swersky