Functions that don't require new class declaration

Posted on 2006-05-07
Last Modified: 2012-05-05

I know this is a very simple thing to do, as I've done it loads of times before, but it's been soooooo long since I've coded in C# that I really need some refreshing on certain aspects!

Imagine that I've created a function in a class and I want to call that function from elsewhere in my project.  I would do one of the following....

       obj = new Namespace.Class();


       new Namespace.Class().myFunction();

....but, what I really want to do is this....


I'm pretty sure that this boils down to the declaration of the function itself, but can't for the life of me figure out (or succesfully Google) how to do it!

Can someone give me an example please?

Question by:Jaymol
    LVL 48

    Accepted Solution

    Declare this function as static:

    public class Class
        public static myFunction()



    You can see many static functions in .NET like Console.WriteLine().

    C# 8.0 has additional feature: you can declare whole class as static, such class contains only static functions and cannot be instantiated.

    LVL 6

    Author Comment

    Thanks Alex,

    Just one question though.  I did the above, whilst slapping myself for forgetting something so simple, but it wouldn't compile.  I only had to declare the function return type (in this case, void) before it would.  Did you just get the above code a little wrong or would you expect it to have worked as you showed?

    Thanks again,

    LVL 48

    Expert Comment

    Sorry, it was my mistake, you fixed it.
    LVL 6

    Author Comment

    Cheers big ears.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now