Calling the base class constructor

  • Thread starter Thread starter Jo Vermeulen
  • Start date Start date
J

Jo Vermeulen

Hello,

I was wondering how I could call the base class constructor from a derived
class constructor. In Java I could do something like

super(parameter);

I tried

base(parameter);

in C# but it doesn't compile. Any ideas?

Thanks in advance!
 
Example :

public class BaseClass
{
public BaseClass(int i, int j)
{
...
}
}

public class DerivedClass : BaseClass
{
public DerivedClass(int i, int j, int k) : base(i, j) //this is
where you call base constructor
{
...
}
}
 
I was wondering how I could call the base class constructor from a
derived
You do base (parameter) but in a different place:

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

See http://www.pobox.com/~skeet/csharp/constructors.html for more
information.


I've never understood why this special syntax is used here. Why not do it
like java and allow base(param);?
This would give you the freedom to do parameter processing before calling
the base ctor or you could do a try{}catch around the base()-call in future
versions of c#.
 
cody said:
I've never understood why this special syntax is used here. Why not do it
like java and allow base(param);?
This would give you the freedom to do parameter processing before calling
the base ctor or you could do a try{}catch around the base()-call in future
versions of c#.

Just having the Java syntax wouldn't allow that - Java doesn't allow
anything else to happen before the constructor is called either.

For parameter processing, the normal approach is to write a static
method which massages the parameters appropriately, and use

public Foo(int x) : base (Massage(x))
{
....
}
 
I've never understood why this special syntax is used here. Why not do
it
Just having the Java syntax wouldn't allow that - Java doesn't allow
anything else to happen before the constructor is called either.

I know, but this syntax would allow extensions for C# compilers in future to
do something like:

public GifFile(string s)
{
try
{
base (GetCurrentPath() + s + ".gif");
}
catch (IOException e)
{
base(); // call the empty ctor to create empty GIF which can be
filled dynamically
}
}

Theoretically this would bee possible, MSIL allows executing code before
ctor is called and call to multiple base ctors.
For parameter processing, the normal approach is to write a static
method which massages the parameters appropriately, and use

public Foo(int x) : base (Massage(x))
{
...
}

Good idea but it requires an additional method.
 
cody said:
I know, but this syntax would allow extensions for C# compilers in future to
do something like:

public GifFile(string s)
{
try
{
base (GetCurrentPath() + s + ".gif");
}
catch (IOException e)
{
base(); // call the empty ctor to create empty GIF which can be
filled dynamically
}
}

Theoretically this would bee possible, MSIL allows executing code before
ctor is called and call to multiple base ctors.

It would allow for that syntax, yes - but whether they really want to
be encouraging that is a different matter.
Good idea but it requires an additional method.

Sure - I was just giving a workaround for the current restrictions in
some cases.
 
Back
Top