why use the static function ?

  • Thread starter Thread starter Kylin
  • Start date Start date
Static methods enable you to call code without an instance of the class in
which it's defined.

You cannot use instance data in a static method.

A static method should be self-contained. This is to say that it will
perform its function without requiring or saving any stateful data. A
classic static method would be something like...

public static double Multiply(double a, double b)
{
return a*b;
}

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Static functions are used in situations where you want to share some
Business Logic code over and over in your UI. Think of them as global
functions. You dont have to create an instance to access the code thus
reducing lot of overhead.

puclic class MyBusinessLogic
{
public static DataTable GetUniqueCustomerList()
{
}
}

In your UI Code you can call-
DataTable myTable=MyBusinessLogic.GetUniqueCustomerList();
 
<groan>

If you try really hard you can write a whole application using only static
methods and ensuring the maximum of spaghetti code too.

Static functions should be used only where the architecture demands a clean
and stateless system of operation. The Math class in .NET is a primary
example of good use of static methods.

Using static as a replacement for Global is a recipie for disaster and a
sign of one of two things. #1 having learned programming with classic VB and
#2 having no clue about object oriented architecture.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Using static as a replacement for Global is a recipie for disaster and a
sign of one of two things. #1 having learned programming with classic VB
and #2 having no clue about object oriented architecture.

Thanks nothanks for obnoxious remarks.
We have been developing c/c++ enterprise software for quite some time. We
very well understand that static functions are used for utility/helper
functions only.
 
Back
Top