Results 1 to 4 of 4

Thread: Invoking Base Class Constructor [Resolved]

  1. #1

    Thread Starter
    Addicted Member Kezmondo's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    166

    Invoking Base Class Constructor [Resolved]

    I'm very new to C# and have a quick question...

    When a class is derived from a base class, is the base class constructor always invoked when an instance of the derived class is created, regardless of whether ": base()" is added to the derived class constructor?


    e.g I have a "Control" class, and a "Listbox" class which dervies from it.
    When I create an instance of "Listbox", is the "Control" constructor always invoked?

    Thanks...
    Last edited by Kezmondo; Sep 5th, 2005 at 05:39 AM.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Invoking Base Class Constructor

    It calls the default constructor automaticaly. If you need to call an other constructor, then you have to do it manualy. A test like this reveals it.


    Code:
    using System;
    
    namespace testbase{
    
    	class Class1{
    
    		static void Main(string[] args){
    			
    			Class2 test1 = new Class2();
    			Class3 test2 = new Class3();
    			Class2 test3 = new Class3();
    
    			Console.WriteLine("test1: " + test1.hello);
    			Console.WriteLine("test2: " + test2.hello);
    			Console.WriteLine("test3: " + test3.hello);
    
    			Console.ReadLine();
    		}
    	}
    
    
    
    	class Class2{
    
    		public string hello;
    
    
    		public Class2(){
    			hello = "hello";
    		}
    	}
    
    
    
    	class Class3 : Class2{
    
    		public Class3(){
    
    		}
    
    	}
    }

    - ØØ -

  3. #3

    Thread Starter
    Addicted Member Kezmondo's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    166

    Re: Invoking Base Class Constructor

    Great, thanks.

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Invoking Base Class Constructor [Resolved]

    No, problem, just happy to help out.


    - ØØ -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width