Tutorial :What is a “static” class?



Question:

In C# what is the difference between:

public static class ClassName {}  

And:

public class ClassName {}  


Solution:1

A static class cannot be instantiated, and can contain only static members. Hence, the calls for a static class are as: MyStaticClass.MyMethod(...) or MyStaticClass.MyConstant.

A non static class can be instantiated and may contain non-static members (instance constructors, destructor, indexers). A non-static member of a non-static class is callable only through an object:

MyNonStaticClass x = new MyNonStaticClass(...);  x.MyNonStaticMethod(...);  


Solution:2

Firstly, a comment on an answer asked about what "static" means. In C# terms, "static" means "relating to the type itself, rather than an instance of the type." You access a static member (from another type) using the type name instead of a reference or a value. For example:

// Static method, so called using type name  Guid someGuid = Guid.NewGuid();  // Instance method, called on a value  string asString = someGuid.ToString();  

Now, static classes...

Static classes are usually used as "utility" classes. The canonical example is probably System.Math. It doesn't make sense to create an instance of math - it just "is". A few rules (both "can" and "can't"):

  • Static classes always derive from object. You can't specify a different base type, or make the static class implement an interface.
  • Static classes can't have any instance members - all variables, methods etc must be static.
  • Static classes can't declare any instance constructors and the compiler doesn't create a parameterless constructor by default. (Before static classes came in C# 2.0, people would often create an abstract class with a private constructor, which prevented instantiation. No need here.)
  • Static classes are implicitly abstract (i.e. they're compiled to IL which describes an abstract class) but you can't add the abstract modifier yourself.
  • Static classes are implicitly sealed (i.e. they're compiled to IL which describes an sealed class) but you can't add the sealed modifier yourself.
  • Static classes may be generic.
  • Static classes may be nested, in either non-static or static classes.
  • Static classes may have nested types, either non-static or static.
  • Only static, top-level non-generic classes can contain extension methods (C# 3.0).


Solution:3

A static class also can not be inherited from, whereas a non-static class with static members can be inherited from.


Solution:4

public static class ClassName {}  

A static class is just like a global variable: you can use it anywhere in your code without instantiating them. For example: ClassName. After the dot operator, you can use any property or function of it.

 public class ClassName {}  

But if you have non-static class then you need to create an instance of this class. For example:

 ClassName classNameObject = new ClassName();   


Solution:5

All methods/properties in a static class must be static, whereas a 'normal' class can contain a mix of instance and static methods.


Solution:6

You can't instantiate (create objects of) a static class. And it can only contain static members.

Example: System.Math


Solution:7

Static class can contain static members only.

Static member can be used without instantiating a class first.


Solution:8

Static classes and members are used to create data and methods that can be accessed without creating an instance (using the new keyword, they cannot have a constructor) of the class.

Static classes can be declared when there is no dependence on the its own object identity, so a static class must contain only static members.

This classes are loaded by the CLR when the program or namespace containing the class is loaded.

They are also sealed, cannot be inherited from.


Solution:9

http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html - very good article on this. This is for Java. But i think concept should should same in C# too.


Solution:10

Static variable in c

a variable local to a class as auto variables but static variable do not disappear as function is no longer active.Their values persist.If control comes back,static variables have same value

static function in c functions that are not visible to functions in other files.

*static data members in cpp * data members can be variables or functions in cpp static applies to both data members the class itself can be static "There is only one copy of static data memberss shared by all objects in that class" static data members can access only static data members

static class this class cannot instantiate objects


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »