Loading ...

Static Classes and members in C#?

Who is online?  0 guests and 1 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Static Classes and members in C#?

Static Classes and members in C#?

Posts under the topic: Static Classes and members in C#?

Posted: 9/12/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Hello Experts,

I have searched on Google but didn't find the best answer.So I suppose to ask on codeasp as there are lots of topgun of C#.

My question is what is main benefits of Static Keyword in C# as I know that we can use static members without creating an instance of the class. Can anyone tell me where we should use static class and static functions?

Thanks in advance.


Posted: 9/13/2010

Professional 8338  points  Professional
  • Joined on: 4/15/2009
  • Posts: 424
  Answered

mohit said:

user="mohit kumar"]Can anyone tell me where we should use static class and static functions?

Static class are usually used if you are creating a helper/utility class.

Give this short discussion a read: http://forums.asp.net/t/1580582.aspx


Posted: 9/13/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

Besides the Vinz's answer and the nice conversation where both @Steve and @Mikesdotnetting have given very good examples, I would like to add the following notes:

- YOU HAVE ONLY STATIC CONSTRUCTORS / NO INSTANCE CONSTRUCTOR

Usually, in non-static classes, the constructor is called once the object is instantiated i.e.

MyClass cls = new MyClass(); <-- in this moment, the constructor of the MyClass class is called. However, as you already know, the static class is not instantiated, which means, no use of constructors!!! :)

then

cls.MyMethod(params);

If MyClass was static one, you would call it like this:
MyClass.MyMethod(params);

 

OR when we have non-static class with static constructor, only static fields can be ued inside the static constructor.

see this example:

    public class MyParent
    {        
        public static string y;
        string x;
        static MyParent()
        {
            y = "hajan";
            x = "hajan"; //not possible
        }            
    }

If See also: Instance constructor

 

- (as said previously) OBJECT IS NOT INSTANTIATED, ONE CODE-LINE LESS & OBJECT RESOURCES LESS!

Yes, each instance of one created object of a Class occupies separate memory space AND the values of its fields EXCEPT STATIC FIELDS, also occupies a memory space since the values of its fields are separate and independent.

One of the good example implementations if when creating Helper Classes.

Example:
In one of my latest projects, I've created small Helper class with name SQLOperations that uses many static methods such as LoadData(someparams) which returns DataTable. Each time I want to get DataTable with particular Data I just do this:

DataTable myDt = SQLOperations.LoadData(params);

 

- NO UNIQUE OBJECTS OF THAT CLASS

You can't have

Person person1 = new Person();
Person person2 = new Person();

because static class provide functionality that is not specific to a given object of the class.

 

- STATIC CLASS IS SEALED CLASS

You cannot add the sealed keyword because it's by default sealed :). Sealed class is the class which cannot be derived by another class.

 

- IN MOST OF THE CASES, WE USE NON-STATIC CLASS WITH STATIC MEMBERS.

Example:

public class Person
{

public static void GetHeight()
{
  //
}

public void Add(string smth)
{

}

}

 

BENEFITS

- Useful for performance and efficiency reasons
- Nice to use when creating extension methods to an existing library

 

Hope this helps.
Hajan

 

P.S. Probably I will write a blog with some better formattings, later. :)


Posted: 9/13/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Superbbbbb reply Hajan.Thank you so much Hajan. Now I must write a blog on it before youCool


Posted: 9/13/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Vinz said:

user="Vincent Maverick Durano"]

 

mohit said:

user="mohit kumar"]Can anyone tell me where we should use static class and static functions?

 

Static class are usually used if you are creating a helper/utility class.

Give this short discussion a read: http://forums.asp.net/t/1580582.aspx

 

Thanks Vinz:)


Posted: 9/13/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

I have also read about static and want to share with you. If I am wrong, please tell me.:)

  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The compiler will guarantee that instances of the static class cannot be created. 
  • Static class cannot be inherited. 

 


Posted: 9/13/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

mohit said:

user="mohit kumar"]

I have also read about static and want to share with you. If I am wrong, please tell me.:)

 

  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The compiler will guarantee that instances of the static class cannot be created. 
  • Static class cannot be inherited. 

 

If I read well, your statements are correct!

And, yes, static class cannot be inherited because it's a sealed class ;)!


Posted: 9/13/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

And, here is the blog post: http://codeasp.net/blogs/hajan/microsoft-net/938/back-to-basics-static-class-static-members-in-c-explained

I hope this will be useful to someone else out there ;)!


Page 1 of 1 (8 items)