Why we use Static Class

Posted by Nksingh420 under C# on 3/29/2013 | Points: 10 | Views : 4453 | Status : [Member] | Replies : 2
I know what is static class but i want to know why & where we will use static class




Responses

Posted by: Satyapriyanayak on: 3/29/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You would use a static class if:

1. You just want a class to contain a bunch of static or constant members.

2. You don't want it to be possible to instantiate the class.

3. You don't want it to be possible to inherit from the class.

In effect, static classes are the nearest thing we have to procedural programming in an otherwise object oriented language.

For example:

using System;

static class Program
{
const string Hello = "Hello"; // it's OK for a static class to include a constant

static void Main() // all members of a static class must be static, const or nested types
{
Console.WriteLine("The Main method is in a static class");
Program P = new Program(); // compiler error - can't instantiate a static class
}

class Yourclass // it's OK for a static class to include a nested type
{
}
}

class MyClass : Program // compiler error - can't derive from static class
{
}

If this post helps you mark it as answer
Thanks

Nksingh420, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandeepmhatre on: 4/3/2013 [Member] Starter | Points: 25

Up
0
Down
hi,

Refer this link

http://sandeepmhatre.blogspot.com/2012/12/static-members.html

Sandeep M,
Software Developer
Follow me on :
http://sandeepmhatre.blogspot.in

Nksingh420, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response