Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on why we create static class in c# within the ASP.NET forums, part of the Web Development category; what is the main benefit and purpose of static class...
|
|||||||
|
|||
|
Re: why we create static class in c#
At first, by declaring the variable or a method as static means all the objects will get the same copy of it.
Also Static variables will be placed on the stack thereby making a global copy which each and every object of that class can access. If a class is declared as static then the variables and methods should compulsorily be declared as static. 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. Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods. ->The main features of a static class are: 1. They only contain static members. 2. They cannot be instantiated. 3. They are sealed. 4. They cannot contain Instance Constructors or simply constructors as we know that they are associated with objects and operates on data when an object is created. [C# e.g.] static class CollegeRegistration { //All static member variables static int nCollegeId; //College Id will be same for all the students studying static string sCollegeName; //Name will be same static string sColegeAddress; //Address of the college will also same //Member functions public static int GetCollegeId() { nCollegeId = 100; return (nCollegeID); } //similarly implementation of others also. } //class end public class student { int nRollNo; string sName; public GetRollNo() { nRollNo += 1; return (nRollNo); } //similarly .... public static void Main() { //Not required. //CollegeRegistration objCollReg= new CollegeRegistration(); //<ClassName>.<MethodName> int cid= CollegeRegistration.GetCollegeId(); string sname= CollegeRegistration.GetCollegeName(); } //Main end } Last edited by abhishek_gp3; 08-03-2009 at 04:24 PM. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| what is the use of Beaninfo class and introspector class in java beans? | anveth | Java | 0 | 11-24-2008 01:36 AM |
| Object for static class | dsureshkumar28 | Java | 3 | 09-08-2008 01:14 PM |
| Difference Bettween Class.forName(classname)& new class name | prakashreddy.gade | Java | 1 | 05-22-2008 10:24 AM |
| Static and Singleton Class | Geek_Guest | C and C++ | 1 | 11-27-2007 06:26 AM |
| Cannot Identify Object of Class Static | gayathri | QTP | 3 | 06-14-2007 02:29 PM |