Home > Articles > C# Articles > Static classes and static members in C#

Static classes and static members in C#

by Spidy   on Jan 10, 2011   Category: C#  | Level: Beginner  |  Views: 451    |  Points: 100   
Like this article? Bookmark and Share:
In this article you will learn how to use static classes and methods in the .net projects.




 

Introduction


The static classes and static members can be accessed without creating the object of the specific class. We cannot create the object for the static class. Non static methods only accessed through the instance of the class. if the class declared as a static and that class must contain only static members. By default static classes arce sealed. 

Generally the static methods are used to create static utility methods in the project as shown below

public static class GetInfo

{

        public static string GetFormattedDate(string Date)

        {

            try

            {

                DateTime dt = Convert.ToDateTime(Date);

                return dt.ToString("dd/MM/yyyy");

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        public static string GetUserDetails()

        {

            //....return user info

        }

       

}

Above is an sample static classes contains two methods that return date format and user details. Below is the way to accessing static methods.

Accessing static methods

string formatdDt = GetInfo.GetFormattedDate(DateTime.Now.ToString());

string Userdet = GetInfo.GetUserDetails();



Conclusion


In this way we can use static classes and utility methods in the projects.

 

User Responses | Post Article

 No Response found!
  Most viewed Articles
* Programmatically Update the UpdatePanel using ASP.NET (827) by Shiva on May 15 2011 9:16AM
* How to bind dropdown list with XML file in asp.net? (806) by Thamilselvanj on Aug 29 2011 12:23AM
* What is windows service? How to create windows service using .Net? (653) by Thamilselvanj on Nov 14 2010 9:48PM
* What is XML? (547) by Narain_Siddharth on Dec 19 2010 2:24AM
* Static classes and static members in C# (451) by Spidy on Jan 10 2011 12:28PM
 Submit feedback about this article
Please sign in to post feedback