Functions that don't require new class declaration

Posted on 2006-05-07
Medium Priority
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
  • 2
  • 2
LVL 48

Accepted Solution

AlexFM earned 400 total points
ID: 16624525
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.


Author Comment

ID: 16624541
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

ID: 16624588
Sorry, it was my mistake, you fixed it.

Author Comment

ID: 16624614
Cheers big ears.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

840 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