Results 1 to 8 of 8

Thread: why we create static class in c#

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Answers
    1

    why we create static class in c#

    what is the main benefit and purpose of static class


  2. #2
    Junior Member
    Join Date
    Jun 2009
    Answers
    1

    Re: why we create static class in c#

    because static class methods can be callled without creating object


  3. #3
    Junior Member
    Join Date
    Jul 2009
    Answers
    1

    Re: why we create static class in c#

    An example with explanation would be greatly appreciated..

    Thanks,


  4. #4
    Junior Member
    Join Date
    Apr 2006
    Answers
    1

    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();

    //.
    int cid= CollegeRegistration.GetCollegeId();
    string sname= CollegeRegistration.GetCollegeName();


    } //Main end
    }

    Last edited by abhishek_gp3; 08-03-2009 at 03:24 PM.

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Answers
    5

    Re: why we create static class in c#

    I would also add that creating or instantiating classes every time you need to access a method, is very wasteful.


  6. #6
    Junior Member
    Join Date
    Jun 2010
    Answers
    2

    Re: why we create static class in c#

    Static class variable is shared for whole class .
    No need to create object of class to access it.
    It is accessed by Class name directly.E.g Class.staticvar


  7. #7
    Junior Member
    Join Date
    Jun 2010
    Answers
    1

    Re: why we create static class in c#

    Quote Originally Posted by sanjay8n View Post
    what is the main benefit and purpose of static class
    We create static class, because we call it's method directly without create it's object.


  8. #8
    Junior Member
    Join Date
    Feb 2009
    Answers
    9

    Re: why we create static class in c#

    The main advantage of Static Classes come when there is a need to use WebMethod or a webservice, so that the application can call the static method without creating an object. But there are few drawbacks of static methods.. Static methods can use session values but cannot use viewstate values in it..


  9.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact