add a public static method to a class in csharp

To add a public static method to a class in C#, you need to use the public and static access modifiers in the method declaration. Here's an example:

main.cs
public class MyClass
{
    // Instance variables and methods

    public static void MyStaticMethod()
    {
        // Code goes here
    }
}
142 chars
10 lines

In the above example, the keyword public specifies that the method is accessible from outside the class, and the keyword static specifies that the method belongs to the class itself, rather than to any instance of the class.

You can then call the static method from anywhere in your code by referencing the class name, like this:

main.cs
MyClass.MyStaticMethod();
26 chars
2 lines

gistlibby LogSnag