Thread: Accessing Class members without Instantiating

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Accessing Class members without Instantiating

    Hello,
    I was doing some C# the other day..and I wanted to print something to the screen..
    System.Console.Writeline(..)
    now I get System is a namespace, but Console is a freaking class
    how is it possible to access a method directly from a class name?
    I was reading something that it is a static class but I don't fully understand..
    but it woul make sense to me if this type of class can only contain methods but should be used to store data, unless the data is to detect success of operation..
    can anyone clarify?
    Thanks.
    You ended that sentence with a preposition...Bastard!

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    A static class allows you to access it's methods without having to create an instance of the actual class.

    For example, if you want a group of helper functions/methods that you can use across the whole program. You could simply create a static class called HelperMethods, and create the methods within them. You can then simply call the actual methods without creating an instance of a class. Such as: HelperMethods.Method();

    HAve a look at this regarding data members within static classes: http://www.csharphelp.com/2006/04/c-static-members/
    Last edited by darren78; 12-27-2010 at 08:14 AM.

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by darren78 View Post
    A static class allows you to access it's methods without having to create an instance of the actual class.

    For example, if you want a group of helper functions/methods that you can use across the whole program. You could simply create a static class called HelperMethods, and create the methods within them. You can then simply call the actual methods without creating an instance of a class. Such as: HelperMethods.Method();

    HAve a look at this regarding data members within static classes: C# – Static Members | C# Help – C# Tutorials and Resources
    Hi, thanks for the reply.
    I read the notes in the link, and I get the stuff about the static data members/functions..

    if Writeline() and functions in the Console class are static then it makes sense how they are accessed in that way....
    but does that static Class occupy memory?
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Eman View Post
    Hi, thanks for the reply.
    I read the notes in the link, and I get the stuff about the static data members/functions..

    if Writeline() and functions in the Console class are static then it makes sense how they are accessed in that way....
    but does that static Class occupy memory?
    Yes. The instructions for the method itself will still reside in memory.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Are there not many C# programmers? I didn't expect any replies..
    em, thanks I think I will quit learning C#!
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class type help
    By static-x in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2009, 03:26 PM
  2. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 03:01 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 10:56 AM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM