Confused about line of code in book

R

Rene

I am reading a book that contains one line of code that I don't understand.
The line of code is supposed to be a constructor and is this:

protected NameValueList(SerializationInfo info, StreamingContext context) :
base(info, context)
{
}

This constructor is supposed to be necessary because the class inherits from
a class that implements ISerializable. The primary thing that I would like
to understand is the

: base(info, context)

part, what is the ":" doing there, is that some kind of inheritance or what?

Thank you.
 
F

Frans Bouma [C# MVP]

Rene said:
I am reading a book that contains one line of code that I don't understand.
The line of code is supposed to be a constructor and is this:

protected NameValueList(SerializationInfo info, StreamingContext context) :
base(info, context)
{
}

This constructor is supposed to be necessary because the class inherits
from a class that implements ISerializable. The primary thing that I would
like to understand is the

: base(info, context)

part, what is the ":" doing there, is that some kind of inheritance or what?

No, that's the delimiter for the call to the base class' constructor. So a
shortcut to avoid having to call the base class' constructor from the
constructor's body.

Frans.
 
R

Rene

No, that's the delimiter for the call to the base class' constructor. So a
shortcut to avoid having to call the base class' constructor from the
constructor's body.

Just out of curiosity, how would you call the base constructor from the
constructor's body? I tried the following and it did not work:

protected NameValueList(SerializationInfo info, StreamingContext context)
{
base(info, context)
}

Also if you have a minute, would you be so kind as to give me one or two
other examples on how to use this shortcut in other scenarios?

Thank you.
 
F

Frans Bouma [C# MVP]

Rene said:
Just out of curiosity, how would you call the base constructor from the
constructor's body? I tried the following and it did not work:

protected NameValueList(SerializationInfo info, StreamingContext context)
{
base(info, context)
}

I formulated my sentence a bit ambiguistic, I'm sorry: I meant: the shortcut
/ construct is there to avoid having the developer to call the base class'
constructor from the constructor's body. This has an advantage: you know
where the call to the base class constructor is stated and that it is done at
the right moment: BEFORE the constructor of the derived class is executed.
Having a construct like in VB.NET, where you have to do MyBase.New() can lead
to errors if you expect some base class setup in your derived class but it
isn't there yet as the base class' constructor isn't called yet.
Also if you have a minute, would you be so kind as to give me one or two
other examples on how to use this shortcut in other scenarios?

If you have various overloads of the constructor, it is important to call
the right base class' constructor from the derived class constructors. In
these situations it is used. With classes which have constructors without
parameters, you don't need the construct as it is added for you if you don't
specify it.


Frans.
 
R

Rene

Thanks. I think I got it.

Frans Bouma said:
I formulated my sentence a bit ambiguistic, I'm sorry: I meant: the
shortcut
/ construct is there to avoid having the developer to call the base class'
constructor from the constructor's body. This has an advantage: you know
where the call to the base class constructor is stated and that it is done
at
the right moment: BEFORE the constructor of the derived class is executed.
Having a construct like in VB.NET, where you have to do MyBase.New() can
lead
to errors if you expect some base class setup in your derived class but it
isn't there yet as the base class' constructor isn't called yet.


If you have various overloads of the constructor, it is important to call
the right base class' constructor from the derived class constructors. In
these situations it is used. With classes which have constructors without
parameters, you don't need the construct as it is added for you if you
don't
specify it.


Frans.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top