none
Newbie-question about C# instances vs classes RRS feed

  • Question

  • Greetings.

    I've always understood that in a OO-environment actual actions were carried out by instances and that classes were more a kind of prototypes. You first had to instantiate an object from a class, before you could actually have an action performed. However, in all those C# "Hello World"-examples I never see an object containing Main being instantiated. There's only a class being defined and yet... it works. How come? Am I missing something?

    Willem.

    Sunday, December 30, 2012 3:12 PM

Answers

  • That's because the method Main is a static method. Thus, you can run it without creating an instance, it doesn't depend on instances.

    public class Person
    {
        public int height;
        public static int defaultHeight = 180;
    
        public Person()
        {
            height = defaultHeight;
        }
        public Person(int height_in_cm)
        {
            height = height_in_cm;
        }
    }
    class Program
    {
        static void Main()
        {
            Person bob = new Person();
            Console.WriteLine(bob.height == Person.defaultHeight);
            
            Person sara = new Person(170);
            Console.WriteLine(bob.height - sara.height);
            Console.WriteLine(Person.defaultHeight - sara.height);
            Console.ReadKey();
        }
    }
    This example shows static and non-static fields instead of methods, but the idea is the same. "Normal" members (non-static) need references, instances, they might be different for every instance; while static members are, well, static, independent of instances.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    • Marked as answer by Willem Vermeer Sunday, December 30, 2012 3:42 PM
    Sunday, December 30, 2012 3:28 PM
  • Because Main is a static method and therefore does not require an instance of a class.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by Heslacher Sunday, December 30, 2012 3:19 PM
    • Marked as answer by Willem Vermeer Sunday, December 30, 2012 3:42 PM
    Sunday, December 30, 2012 3:17 PM

All replies

  • Because Main is a static method and therefore does not require an instance of a class.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by Heslacher Sunday, December 30, 2012 3:19 PM
    • Marked as answer by Willem Vermeer Sunday, December 30, 2012 3:42 PM
    Sunday, December 30, 2012 3:17 PM
  • That's because the method Main is a static method. Thus, you can run it without creating an instance, it doesn't depend on instances.

    public class Person
    {
        public int height;
        public static int defaultHeight = 180;
    
        public Person()
        {
            height = defaultHeight;
        }
        public Person(int height_in_cm)
        {
            height = height_in_cm;
        }
    }
    class Program
    {
        static void Main()
        {
            Person bob = new Person();
            Console.WriteLine(bob.height == Person.defaultHeight);
            
            Person sara = new Person(170);
            Console.WriteLine(bob.height - sara.height);
            Console.WriteLine(Person.defaultHeight - sara.height);
            Console.ReadKey();
        }
    }
    This example shows static and non-static fields instead of methods, but the idea is the same. "Normal" members (non-static) need references, instances, they might be different for every instance; while static members are, well, static, independent of instances.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    • Marked as answer by Willem Vermeer Sunday, December 30, 2012 3:42 PM
    Sunday, December 30, 2012 3:28 PM
  • There are two type of classes. 

    1) static class

    2) non static class

    As a beginner you can say that you will use static class when you are sure that you will use it just one time in your code and you will use non static class when you know that you will use it more than one time in your code. You also have to pay attention to access modifiers. Those are also important if you want to make sure that your classes will be accessed from some part of code or not.


    Microsoft Student Parnter Microsoft Technology Associate

    Sunday, December 30, 2012 3:46 PM
  • Anyone know a good book or set of resources to assist an excellent VB/VBA programmer in learning C# .NET ?

    Sunday, December 30, 2012 7:29 PM
  • Today the features are very similar. Besides, they both use .NET, so you're more than half the way there. The only thing left is the syntax, which requires no more than basic information, available in the MSDN library and in some tutorials.

    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    Monday, December 31, 2012 12:51 PM