c# – C # Methods call types

Question:

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

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

What is the origin of this ToShortDateString() method?

Another example:

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

How can a method "call" another method, what is the name of that?

Answer:

Imagine we have a method that returns a String:

public string StringMarota()

I can call this method and throw the result into a variable:

var marota = StringMarota();

Every string has the ToString method, right?

var manola = marota.ToString();

The point is that you're not calling one method on another, you're calling a method on the return of the previous method. Returning to our example:

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

Can be reduced to:

var manola = StringMarota().ToString();

As long as each method has a return, you can chain the calls together.

Scroll to Top