what is meaning of function().variable sysnax in c#

Started by
3 comments, last by Nypyren 5 years, 11 months ago

hi. as I say it in many implemenations and libraries in c# that there is a dot after a function nad it may have properties variables or functions like as I use like below in unity: 

 

SceneManager.GetActiveScene().name

how it is implemented in a class and what is it?

 

Advertisement

GetActiveScene() returns a Scene. This Scene has various properties, e.g. name.

It's the same as doing the following, just shorter to write:


Scene scene = SceneManager.GetActiveScene();
scene.name

 

Hello to all my stalkers.

In cases like that, the function is returning an object that has it's own methods and any public members. So, if GetActiveScene() returned Scene object, then GetActiveScene().name would be equivalent to Scene.name

So, in short, Function() { return object}

Just like a complex expression like x * y * z * w is made up of x*y, the result *z, the result *w, you can also chain multiple '.' operators together, or even include other operators as well.  As long as the type of one of the sub-expressions isn't void, you can continue applying more operators.

SceneManager.GetActiveScene().name.IndexOf('A') + 5;

You can even do crazier stuff like return functions and then immediately call them:

FunctionThatReturnsAFunction()();

This topic is closed to new replies.

Advertisement