Hi,
This blog post summarizes the new features of C# 2.0.
Below are the new features been introduced.
§ Partial Classes
§ Static Classes
§ Generic Classes
§ Anonymous methods
§ Iterators
§ Nullable Types
Static Classes
- Static classes give us a faster way of implementing classes who's only purpose is to deliver static members. Reason - The .Net framework maintains a single object in memory for static members.
- Though you can always just create a class with a private constructor, so that it doesn't get instantiated, and then add all static members into it, Static classes enforce these requirements automatically.
- Static classes are sealed (they can't be derived from), they can't be instantiated.
- Static classes can't contain an instance constructor. They can only have ‘Static’ constructor, but without access specifier.
- The main reason would be: Improved Performance.
Ø Static access means that the instantiation of an object is unncessary. The .Net framework maintains a single object in memory for static members.
Ø A static class is more of a rule: it means that you can't instantiate it as an object and that it can only have static members.
Ø Use static members whenever possible and only use instantiated class objects whenever more than copy of the object will be necessary to carry the intended function of the project.
- Advantages of Static Constructor
Ø The static constructor is called just before the first time any instance method or any class method is called.
Ø This is also a thread safe way of implementing a singleton, because the class constructors are thread safe in .NET.
public class Singleton
{
private static Singleton _instance;
static Singleton()
{
instance = new Singleton();
}
...
}
Hope this helps
Thanks & Regards,
Arun Manglick || Senior Tech Lead
No comments:
Post a Comment