Share via


Why we can not Create instance of static class?

Question

Thursday, August 1, 2013 12:41 AM

Hi Guys,

  When to use static class & Why we can not create instance of static class?

Thanks in advance

All replies (10)

Thursday, August 1, 2013 12:48 AM ✅Answered

That's by design:

Static Classes and Static Class Members (C# Programming Guide)

You can define a class as static if you want to guarantee that it can't be instantiated, can't derive from or serve as the base for another type, and can contain only static members.


Thursday, August 1, 2013 12:52 AM ✅Answered

What is instance variable?

like if i wrote the class like below

class test
{
    int a,b;
   
   public test()
   {
     
   }

}

then the variables a and b is kown as intance variable of class test.

it can be of any type.........


Thursday, August 1, 2013 12:47 AM

Dear Friend..,

    Because there is no instance variable, you access the members of a static class by using the class name itself. 

Thanks & Regards,

Dhanna


Thursday, August 1, 2013 12:49 AM

What is instance variable?


Thursday, August 1, 2013 12:49 AM

A Good explanation is there in the below link

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.90).aspx


Thursday, August 1, 2013 1:05 AM

Hi,

  I hope this link will help u.

http://msdn.microsoft.com/en-us/library/vstudio/79b3xss3.aspx

http://www.dotnetperls.com/static-class

 


Thursday, August 1, 2013 1:05 AM

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword.

They only contain static members.
They cannot be instantiated.
They are sealed.
They cannot contain Instance Constructors (C# Programming Guide).


Thursday, August 1, 2013 1:09 AM

Hi,

Suppose you have a  1 Ltr. container filled with water. If you need extra 1 Ltr water you need another container called instance. If you dont have another container you just replace old water with fresh called static.

Static class have only single space in RAM and just replace the old value to new one. If you want to create multiple copy [Instance] of a class in memory you need to declare a class not-static and create object [Instance] using "new" keyword.

Hope this help

Thanks


Thursday, August 1, 2013 3:30 AM

Hi,

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Refer below links

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

http://www.dotnetperls.com/static-class

http://stackoverflow.com/questions/7933540/c-sharp-static-class-why-use

http://www.codeproject.com/Articles/11857/Introducing-C-2-0-static-classes


Tuesday, August 6, 2013 9:51 AM

visit this link i hope it will work for you...

http://www.dotnetperls.com/static-class

http://www.thecodekey.com/C_VB_Codes/Abstract_and_Static_Class.aspx