A useful but overlooked (well, at least for me) feature from C# 7 called local function. it is fairly useful for long methods that might requires some self-contained code blocks. Here’s a quick example:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Random number from 1-10: {GetRandomNumber(1,10)}");
Console.WriteLine($"Random number from 1-100: {GetRandomNumber(1, 100)}");
Console.WriteLine($"Random number from 150-200: {GetRandomNumber(150, 200)}");
int GetRandomNumber(int min, int max)
{
Random r = new Random();
return r.Next(min, max);
}
}
}
}
In this example i wrote, i created a local function that generates a random number according to the range i have specified. Do take note that local function also has access to the variables within the method it belongs.