One of new features of C# 7.0 is support for local functions. Local functions are methods that are defined inside other methods to simplify more complex code and by example to support local recursion. This blog post shows how to use local functions in C# 7.0 and gives some advice in context of technical design of code.
To illustrate local function I will use finding of factorial as an example.
class Program
{
static void Main(string[] args)
{
var i = 6;
int Factorial(int n)
{
if (n == 0)
return 1;
else
return n * Factorial(n - 1);
}
Console.WriteLine(Factorial(i));
Console.ReadLine();
}
}
Now one may ask why I use recursive call for factorial as it is not so efficient as using for-loop? Let’s move to for-loop.
class Program
{
static void Main(string[] args)
{
var i = 6;
int Factorial(int n)
{
var result = n;
for (var counter = n - 1; counter >= 1; counter--)
{
result = result * counter;
}
return result;
}
Console.WriteLine(Factorial(i));
Console.ReadLine();
}
}
Now we have more effective method for finding factorials. But it is a simple for-loop. Why should we put it local function?
I think it’s good idea in the means of technical design. This for-loop has one concrete meaning – find the factorial – and this is why I prefer to communicate this fact to developer who is working on the code after me.
How to organize the code?
Now let’s see where we should put local functions in our methods. What I suggest here is my personal opinion. Most important part of every method is method body. So, this is coming as first thing. As we name local functions with informative and descriptive names we can keep these functions in the bottom part of method.
To illustrate my idea the modified version of factorial example is here.
class Program
{
static void Main(string[] args)
{
// method body
if(args.Length == 0)
{
Console.WriteLine("Missing argument: input value as integer");
return;
}
var i = int.Parse(args[0]);
Console.WriteLine(Factorial(i));
Console.ReadLine();
// local functions
int Factorial(int n)
{
var result = n;
for (var counter = n - 1; counter >= 1; counter--)
{
result = result * counter;
}
return result;
}
}
}
Now we have code organized and we benefit from this little trick if we use it everywhere in our codebase where local functions are used.
Some considerations of local functions
Some things to keep in mind when considering local functions:
- Local functions are part of methods where they are defined and they should not contain any system-wide logic.
- Local functions are not visible to other classes and therefore they are tested as unit with methods that own them.
- Use local functions only if there is real need for this.
- Don’t over-use local functions as you may end up with too long methods that are hard to test.
Wrapping up
Local functions are nice feature in C# 7.0 and there are situations where local functions help us to write cleaner code. Still local functions are not a thing to use to shorten or reorganize the code that doesn’t need them. This can heppen when working with spaghetti code. Consider well if local function is needed before going with this and try it out if it seems like solution. Be aware that local functions are not testable as they are considered to be same local unit with containing method.
View Comments (16)
Hi,
what's the advantage of declaring a local function instead of creating an Action or Func as a variable of your method? I guess the aim is to move C# toward a TypeScripty syntax (that is itself moving toward a C-sharpy syntax), but I don't really see the added value, except, maybe, the readability of the code. And even this does not hold as now, we have expression body that replace usual method declarations syntax by lambda expression so...
Your factorial method shouldn't be local as it's something that could obviously be shared elsewhere. You don't provide a compelling argument for the need for local functions at all. What was the point?
To address both replies so far:
Lambdas are more limited than functions. For instance, they cannot do yield return. I wish they'd fix those inconsistencies, but the simpler syntax here is still nice. The yield return problem is actually one of the more compelling use cases for local functions. Generator methods (methods that do yield return) are entirely lazy, which is a problem for doing parameter validation (the validation won't happen when you call the method, but instead will occur when you MoveNext on the enumerable for the first time). For this reason it's a common pattern to break such functions in two with a public function that does the validation and then calls a non-public version that does the iteration. This is ugly, though, and you don't really want any other code to ever call the helper method. Declaring it as a local function solves all of these issues. There's other use cases as well, but this is the most compelling.
Oh, I should have also said in the case of lambdas (Action, Func, etc.) you have to allocate a delegate, which has a runtime cost in both performance and memory, even if it's small. With local functions there's no cost here.
Here is a use case for local functions that I consider more compelling.
void MyFunction()
{
void HandleException()
{
//cleanup code here
}
try
{
//operation that can fail here
}
catch (IOException)
{
HandleException();
}
// many other catch clauses all handled the same way
catch (NotSupportedException)
{
HandleException();
}
}
570010 672553Some genuinely prime posts on this site , bookmarked . 769125
This website features various prescription drugs for ordering online.
Users can easily buy essential medicines from anywhere.
Our catalog includes popular treatments and targeted therapies.
Everything is sourced from licensed providers.
https://community.alteryx.com/t5/user/viewprofilepage/user-id/568982
We ensure discreet service, with data protection and fast shipping.
Whether you're filling a prescription, you'll find what you need here.
Begin shopping today and enjoy stress-free healthcare delivery.
On this platform, you can discover a wide selection of casino slots from leading developers.
Players can enjoy retro-style games as well as new-generation slots with stunning graphics and interactive gameplay.
Even if you're new or an experienced player, there’s a game that fits your style.
play aviator
All slot machines are ready to play anytime and compatible with laptops and mobile devices alike.
All games run in your browser, so you can jump into the action right away.
Site navigation is easy to use, making it quick to browse the collection.
Sign up today, and dive into the world of online slots!
Here, you can easily find real-time video interactions.
Interested in engaging dialogues career-focused talks, this platform has a solution tailored to you.
The video chat feature developed for bringing users together globally.
With high-quality video along with sharp sound, every conversation feels natural.
You can join open chat spaces or start private chats, according to what suits you best.
https://rt.cams18.ru/couples
The only thing needed is a stable internet connection and a device start connecting.
On this site, explore an extensive selection virtual gambling platforms.
Searching for classic games latest releases, there’s a choice to suit all preferences.
The listed platforms checked thoroughly for trustworthiness, enabling gamers to bet with confidence.
1xbet
Moreover, the site offers exclusive bonuses and deals targeted at first-timers including long-term users.
Thanks to user-friendly browsing, discovering a suitable site takes just moments, saving you time.
Stay updated about the latest additions through regular check-ins, as fresh options appear consistently.
¿Quieres cupones vigentes de 1xBet? En nuestra plataforma descubrirás las mejores ofertas para tus jugadas.
El promocódigo 1x_12121 garantiza a hasta 6500₽ durante el registro .
Para completar, utiliza 1XRUN200 y disfruta una oferta exclusiva de €1500 + 150 giros gratis.
https://myworldgo.com/forums/1/news-and-announcements/topic-create
No te pierdas las ofertas diarias para acumular más beneficios .
Los promocódigos listados son verificados para hoy .
Actúa ahora y potencia tus apuestas con la casa de apuestas líder !
Searching for exclusive 1xBet discount vouchers? Our website is your go-to resource to unlock valuable deals designed to boost your wagers.
For both beginners or a seasoned bettor , the available promotions provides maximum benefits during registration .
Keep an eye on seasonal campaigns to maximize your rewards.
https://roomstyler.com/users/888starzbet
All listed codes are tested for validity to work seamlessly this month .
Take advantage of limited-time opportunities to transform your odds of winning with 1xBet.
See what we’re offering https://Dewa-United-FC.com
Embora não tenhamos encontrado nenhum programa VIP específico durante a nossa análise da Parimatch, há muitos bônus e promoções interessantes e disponíveis para todos os seus usuários. Tanto o Parimatch cassino quanto as apostas esportivas em si, são conhecidas por suas ofertas diárias e semanais para usuários novos e antigos. Qualquer pessoa acima de 18 anos pode se inscrever, reivindicar os bônus e claro, ganhar dinheiro na Parimatch. Se este é o seu perfil, vá em frente — a Parimatch tem o nosso selo de confiança. Em resumo: divirta-se com Big Bass Bonanza, mas sempre com consciência. Afinal, o melhor prêmio é jogar com equilíbrio e segurança! Embora não tenhamos encontrado nenhum programa VIP específico durante a nossa análise da Parimatch, há muitos bônus e promoções interessantes e disponíveis para todos os seus usuários. Tanto o Parimatch cassino quanto as apostas esportivas em si, são conhecidas por suas ofertas diárias e semanais para usuários novos e antigos.
https://indusbricks.dreamhosters.com/2025/07/15/review-do-penalty-shoot-out-da-evoplay-multiplicadores-giros-extras-e-bonus-ocultos/
O Big Bass Amazon Xtreme, último jogo desta lista, possui uma temática com elementos da Amazônia brasileira. Com um RTP de 96,07%, suas apostas podem ser multiplicadas até 10000x neste slot de alta volatilidade. Tal como no Big Bass Halloween 2, o Big Bass Christmas Bash tem uma volatilidade muito alta. É uma característica comum aos Big Bass com temáticas festivas. Nesta versão, o slot também chama a atenção do jogador por conta do seu pagamento potencializado. No Big Bass Amazon Xtreme, o apostador pode faturar o valor 10.000 vezes maior que a sua aposta inicial, o que é totalmente atrativo. No cassino ao vivo, encontramos opções como a roleta brasileira, com apostas que variam de R$ 0,50 a R$ 30 mil, e o famoso “Sweet Bonanza CandyLand”, com entradas que vão de R$ 1 a mais de R$ 15 mil.
The game offers flexible betting options ranging from €0.2 to €1,000 per round. This wide range of betting options caters to different player preferences, allowing both low-stakes and high-stakes players to enjoy the game. Whether you’re looking to play with minimal risk or maximize your potential winnings, Chicken Cross provides the flexibility needed to suit various budgets and risk tolerances. The lack of complex bonus features or free spins might be a drawback for some players, but the straightforward gameplay and nostalgic visuals make up for this simplicity. The ability to cash out at any time adds a strategic element, allowing players to maximize their gains by knowing when to quit. Additionally, the game’s popularity on social media platforms like TikTok has contributed to its widespread appeal, making it a fun and interactive experience beyond just individual play.
https://duplexsalon.com/free-entry-balloon-game-tournaments-prize-pool-review/
Teen Patti Master 2025 is the newest edition of the popular Teen Patti game, bringing advanced features, improved graphics, and a more immersive gameplay experience. The 2025 version is tailored to offer a seamless, enjoyable platform for Teen Patti fans, complete with exciting new game modes, social features, and generous bonuses Teen Patti Master 2025 is the newest edition of the popular Teen Patti game, bringing advanced features, improved graphics, and a more immersive gameplay experience. The 2025 version is tailored to offer a seamless, enjoyable platform for Teen Patti fans, complete with exciting new game modes, social features, and generous bonuses Teen Patti Master 2024 is the latest version of the popular Teen Patti card game app, offering updated features, improved graphics, and a smoother user experience. It combines traditional Teen Patti gameplay with new modes, social features, and exciting rewards, making it one of the most immersive Teen Patti apps available.
I once saw remedies as saviors, reaching for them instinctively whenever my body whispered warnings. But life taught me otherwise, revealing how these aids often numbed the symptoms, urging a profound introspection into what wellness truly entails. It stirred something primal, compelling me that mindful engagement with treatments honors our body's wisdom, rather than eroding our natural strength.
During a stark health challenge, I hesitated before the usual fix, uncovering hidden layers that wove lifestyle shifts into medical wisdom. What emerged was transformative: healing thrives in balance, where overdependence dulls our senses. Now, I navigate this path with gratitude to share this insight, seeing medicine as an ally, not a master.
Reflecting on the essence, I now understand interventions must uplift our journey, not overshadow it. This odyssey has been enlightening, inviting us to question our automatic responses for richer lives. It all comes down to one thing: doxycycline vibramycin davis pdf