Types of C#Methods calls

Asked

Viewed 164 times

4

I have a question in the following case, not referring to dates, I used only as an example:

Convert.ToDateTime("01/01/2016 00:00:00").ToShortDateString();

What is the origin of this method ToShortDateString() ?

Another example:

var blablabla = string.Copy("asd").Clone().ToString();

How one method can "call" another method, what is the name of it?

  • 3

    Como um método pode "chamar" outro método, qual o nome disso? As stated in @Juliano’s reply is "call chain". In more specific cases it can also be called build pattern (building pattern)

  • 2

    Thank you to all who answered, if you could mark both answers. Thank you very much for your help.

  • 2

    Possible duplicate of What is Fluent Interface?

  • 1

    @Bacco, this question http://answall.com/questions/106955/o-que%C3%A9-Fluent-interface, does not answer my question, Julian’s answer is exactly what I would like to know. Thank you for your attention.

  • 1

    @Bacco, for a beginner like me, and perhaps for other beginners it is clearer the answer of Juliano than the explanation of what is Fluent Interface.

  • 1

    Agree @Robss70, it seems easier for min that I’m beginner understand the answer below than the Pattern design.

Show 1 more comment

2 answers

6


Imagine we have a method that returns a String:

public string StringMarota()

I can call this method and play the result in a variable:

var marota = StringMarota();

Every string has the method ToString, right?

var manola = marota.ToString();

The point is that you are not calling one method in the other, you are calling one method in the return of the previous method. Returning to our example:

var marota = StringMarota();
var manola = marota.ToString();

May be reduced to:

var manola = StringMarota().ToString();

Since each method has a return, you can link the calls.

3

How one method can "call" another method, what is the name of it?

You are reading the code wrong. The correct one would be to affirm that the result of one method is executing another method.

This code snippet

Convert.ToDateTime("01/01/2016 00:00:00").ToShortDateString();

Can be converted as follows

DateTime date = Convert.ToDateTime("01/01/2016 00:00:00");
string stringDate = date.ToShortDateString();

What happens in the first example is that you are accessing a method that is available in the return of the previously executed method. As the return of the method ToDateTime is a DateTime, you have access to all available instance methods DateTime.

DRT;

Just for information, something close to the quote "How one method can call another method" can be found more easily in javascript, where it is more common to find a function that returns another function. Still, the code is slightly different from the example quoted

function soma(arg1){
   return function(arg2){
      return arg1 + arg2;
   }
}

soma(1)(2); //retorna 3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.