Geeks Talk

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.

why we create static class in c#

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...

Go Back   Geeks Talk > Web Development > ASP.NET
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

ASP.NET ASP.NET and ASP Related Problems

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-06-2009
Junior Member
 
Join Date: Jun 2009
Location: Delhi
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
sanjay8n is on a distinguished road
why we create static class in c#

what is the main benefit and purpose of static class
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-09-2009
Junior Member
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
amnashahid is on a distinguished road
Re: why we create static class in c#

because static class methods can be callled without creating object
Reply With Quote
  #3 (permalink)  
Old 07-22-2009
Junior Member
 
Join Date: Jul 2009
Location: Indiana
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
SRahul123 is on a distinguished road
Re: why we create static class in c#

An example with explanation would be greatly appreciated..

Thanks,
Reply With Quote
  #4 (permalink)  
Old 08-03-2009
Junior Member
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
abhishek_gp3 is on a distinguished road
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.
Reply With Quote
Reply

  Geeks Talk > Web Development > ASP.NET

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 01:42 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved