SIGN UP MEMBER LOGIN:    
ARTICLE

Static and Non-Static Methods in C#

Posted by Abhimanyu Kumar Vatsa Articles | C# Language June 20, 2011
In this quick article you will take a look at static and non-static methods in C#.
Reader Level:

Introduction 

MSDN Definition: A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, we cannot use the new keyword to create a variable of the class type. Because there is no instance variable, we access the members of a static class by using the class name itself. 

C# fields must be declared inside a class. However, if we declare a method or a field as static, we can call the method or access the field using the name of the class. No instance is required. We can also use the static keyword when defining a field. With this feature, we can create a single field that is shared among all objects created from a single class. Non-static fields are local to each instance of an object. 

When you define a static method or field, it does not have access to any instance fields defined for the class; it can use only fields that are marked as static. Furthermore, it can directly invoke only other methods in the class that are marked as static; nonstatic (instance) methods or fields first require creation of an object on which to call them.  

Look at the example given below: 

using System;
using System.Collections.Generic;

using System.Text;

namespace TestiClass
{
   
class Program
   
{
       
public int calculation(int x, int y)
       
{
       
    int val = x * y;
           
return val;

       
}
 

       
static void Main(string[] args)
       
{
           
Program x = new Program();
           
int newval = x.calculation(12,12);
           
Console.WriteLine(newval);
           
Console.ReadKey();
       
}
   
}
}
 

In above example, we have a non-static method named calculation. From the main method (static method), I'm trying to access that non-static method and to do this I need to create the instance of the class inside the main method (static method) because a static method only accesses the static methods or static fields and calculation is not static here. So, to access this I have created an instance (named x) of Program class that has calculation method and then assigning the calculated value to newval field inside static main method. This is really cumbersome task. To fix the inconenience, C# uses static methods. Let's take a look at the use of static here. 

Look at the example given below: 

using System;
using System.Collections.Generic;

using System.Text;

namespace TestiClass
{
   
class Program
   
{
       
public static int calculation(int x, int y)
       
{
           
int val = x * y;
           
return val;
       
}
 
       
static void Main(string[] args)
       
{
           
Console.WriteLine(calculation(12,12));
           
Console.ReadKey();
       
}
   
}
}
 

Try to compare both examples, you will see the difference. Here we have not used any instance of class inside static main method. This is the use of static method in our applications.

Login to add your contents and source code to this article
share this article :
post comment
 

thank u sir it's easy to understand.......for fresher in c#.thanx

Posted by gajanan gogurwar Jul 16, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor