SIGN UP MEMBER LOGIN:    
ARTICLE

Static classes in C#

Posted by Hirendra Sisodiya Articles | C# Language July 28, 2010
In this article you will learn how to use Static classes in c#.
Reader Level:

HTML clipboard

C# provides the important feature to create static classes, there are two main features of a static class, one is no object of static class can be created and another is, a static class must contain only static members, then it is important that what is the main benefit to create a static class, the main benefit of making static class, we do not need to make any instance of this class ,all members can be accessible with its own name.

Declaration:

A static class is created by using keyword 'Static' as shown here:

Static class Clasname
{

  
//C#
}

One more thing that is notable-within static class, all members must be explicitly specified as static, static class does not automatically make its members static. Static class can contain a collection of static methods.

Example:

using System;

 

static class Shape

{

    public static double GetArea(double Width, double height)

    {

        return Width * Height;

    }

}

 

class Ractangle

{

    private void GetRactangleArea()

    {

        Double Area;

        Area = Shape.GetArea(10, 5);

    }

}

Shape is static class, it contain staic function GetArea.Ractangle is other class and with in GetArea function can be access without creating instace of Class Shape.

Although a static class cannot have an instance constructor, it can have a static constructor.

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

Very helpful for me, thanks

Posted by John Burns Dec 17, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • 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
    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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor