PC Review


Reply
Thread Tools Rate Thread

C# Noob: Calling base class constructor

 
 
Richard Coltrane
Guest
Posts: n/a
 
      25th Apr 2007
Hi there,

In vb.net, if i inherit from a class then the first i generally do in a sub
classes contructor is MyBase.New(..);
Whats the pattern in C#? It seems to do these things automatically; I didn't
think C# did "anything" automatically?

Thanks

Richard


 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      25th Apr 2007
>In vb.net, if i inherit from a class then the first i generally do in a sub
>classes contructor is MyBase.New(..);
>Whats the pattern in C#?


The syntax is

public YourClass(...) : base(...)
{
}


>It seems to do these things automatically; I didn't
>think C# did "anything" automatically?


It only calls a parameterless constructor automatically. If you want
to use one with parameters you have to write the code for it.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      25th Apr 2007
"Richard Coltrane" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> In vb.net, if i inherit from a class then the first i generally do in a
> sub classes contructor is MyBase.New(..);
> Whats the pattern in C#? It seems to do these things automatically; I
> didn't think C# did "anything" automatically?


In C# (and many other OOP languages), the default constructor for base
classes is always called automatically. You can use the ": base(...)"
syntax after the constructor declaration in a derived class if you need
access to a base constructor (from the immediate-ancestor class only) with a
specific parameter list.

Had I done any OOP in VB.NET I would have been very surprised to find that
it *didn't* do that. So I guess it all just depends on your perspective.


Pete

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      25th Apr 2007
Richard Coltrane <(E-Mail Removed)> wrote:
> In vb.net, if i inherit from a class then the first i generally do in a sub
> classes contructor is MyBase.New(..);
> Whats the pattern in C#? It seems to do these things automatically; I didn't
> think C# did "anything" automatically?


Others have answered your immediate question, but see
http://pobox.com/~skeet/csharp/constructors.html for more information.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
how to make sure this.Test() call base class method in base class constructor Ryan Liu Microsoft C# .NET 2 27th Aug 2006 07:43 PM
Calling base class default constructor when the base is a generic... SammyBar Microsoft C# .NET 3 16th Jan 2006 06:36 PM
InitializeComponent is generating the wrong code for my custom class. Calling it's base class constructor jrhoads23@hotmail.com Microsoft Dot NET Framework Forms 0 1st Feb 2005 07:10 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
Calling the base class constructor Jo Vermeulen Microsoft C# .NET 7 26th May 2004 10:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:44 AM.