In C # this is not possible, at least not at the moment ( has in C # 7 , see more at another question qui ). You could create delegates , but I doubt it's what you want.
To tell you the truth, I see little use for this. Usually this will only be necessary if the code is too large and the solution is not to create inner functions , as shown in JavaScript. This is done in JS because it is a language that has limitations to limit visibility and scope.
The idea of structuring parts of the code into separate functions is good, separates responsibilities, but does not need to be within another. Create other functions outside of it. The only disadvantage of this is that externally any other method of the class can call it. If it were internal, only she could call the function. But this is not usually a problem.
Of course you should create this function / method, such as private
so it can not be accessed outside the class.
public void Salva() {
if (valida()) insereNoBanco();
}
private bool valida() {
// valida campos
}
private void insereNoBanco() {
// insere dados no banco
}
I would not do this but Daniel's answer put an example of what I initially said in the lambda answer and that is closest to the inner fucntion simulation used in JavaScript:
public void Salva() {
Func<bool> valida = () => {
// valida campos
};
Action insereNoBanco = () => {
// insere dados no banco
};
if (valida()) insereNoBanco();
}
It is unnecessary to do this, it can have unwanted effects and it is not idiomatic in C #.