Newbie Q: Explain: string Name = dept.FindChairperson().Name

  • Thread starter Thread starter Rex
  • Start date Start date
R

Rex

Hi All,
Quick newbie question: I am NOT looking for some code solution - I am
simply looking for an explanation. In a passing bit of code in a C#
book I have, I saw the following line:

string Name = dept.FindChairperson().Name

I AM pretty much getting C# and its various syntaxes, but I had not
seen this before. Here's my question: It is clear that
"FindChairperson" is a method within the dept object. But then
how is ".Name" somehow subservient to the method? In other words,
I think of a class as having methods and properties... but how is it
that they are somehow COMBINED here?

Thanks much,

Rex
 
Quick newbie question: I am NOT looking for some code solution - I am
simply looking for an explanation. In a passing bit of code in a C#
book I have, I saw the following line:

string Name = dept.FindChairperson().Name

I AM pretty much getting C# and its various syntaxes, but I had not
seen this before. Here's my question: It is clear that
"FindChairperson" is a method within the dept object. But then
how is ".Name" somehow subservient to the method? In other words,
I think of a class as having methods and properties... but how is it
that they are somehow COMBINED here?

Okay: dept is a variable, of some type that has a FindChairperson()
method. The return type of the FindChairperson method then has a Name
property. So FindChairperson() might be declared as something like:

Person FindChairperson()

where Person has a Name property.

So your original expression calls FindChairperson and then uses the
Name property of the Person returned by that method.

(I'm deliberately ignoring the fact that it's probably a reference
which is returned, etc, in order to keep things simple.)

Jon
 
Rex said:
Hi All,
Quick newbie question: I am NOT looking for some code solution - I am
simply looking for an explanation. In a passing bit of code in a C#
book I have, I saw the following line:

string Name = dept.FindChairperson().Name

I AM pretty much getting C# and its various syntaxes, but I had not
seen this before. Here's my question: It is clear that
"FindChairperson" is a method within the dept object. But then
how is ".Name" somehow subservient to the method?

The return type of FindChairperson has a property Name of type string (or a
type that is implicitly convertible to string).

I suppose it is a class representing in person and Name is the name of that
person.

To know more, you have to look at the definition resp. documentation of the
type of dept. Maybe the intellisense tells you more.

hth
Christof
 
This is called chaining.

FindChairperson() returns another object, probably a Person or
ChairPerson object, on which the Name property is called. You could
rewrite this as:

ChairPerson chairperson = dept.FindChairperson();
string Name = chairperson.Name;

Often when the intermediate object (chairperson in this case) is not
needed the method/property calls are chained, ie written one after the
other without storing the intermediate objects. Precedence rules make
the "dot" work on the result of the last method call or property in the
chain.

Greetz,

-- Freddy


Rex schreef:
 
Thanks, Alex - But why wouldn't it just be:

string Name = dept.FindChairperson()

But maybe my REAL question is: How would

dept.FindChairperson().Name

actually be coded within the dept class (so that a method is combined
with a property)?

Thanks,

Rex
 
Name property isn't coded within dept class. It is coded in the class, which
is returned by method.
 
HEY, thanks everybody - I think I got it!
The method is returning an object - and that OBJECT has a property
called "Name". All the explanations together helped -and Freddy, I
think your description of this being a "chain" especially helped.
Thanks very much - I really appreciate the excellent explanation,
everyone.
Rex
 

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

Back
Top