.net nested function

T.Zacks 3,986 Reputation points
2022-03-11T10:01:12.377+00:00

i am not familiar with .net nested function. is it local function or something other ? which c# version support nested function and when people go for this nested function....tell me some scenario.

what is advantage or significance of nested function? please guide me. thanks

static void Main(string[] args)
        {
            int level = 0;

            void ListDirectoryContents(string directory)
            {
                level++;

                foreach (var dir in Directory.GetDirectories(directory))
                {
                    Console.WriteLine("{0} {1}", "|".PadRight(level * 3, '_'), Path.GetFileName(dir));
                    ListDirectoryContents(dir);
                }

                foreach (var file in Directory.GetFiles(directory))
                    Console.WriteLine("{0} {1}", "|".PadRight(level * 3, '_'), Path.GetFileName(file));

                level--;
            }

            ListDirectoryContents(Directory.GetCurrentDirectory());
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,386 Reputation points
    2022-03-11T11:10:28.647+00:00

    Usually a local function is used when it's only needed in one method and assisting in keeping the main code clean.

    Simple example, perform validation outside of a lambda statement.

    public static void ReadWrite(char delimitBy = ';')  
    {  
        string[] LineArrayMethod(string lineData)  
        {  
            var lineArray = lineData.Split(delimitBy);  
            return lineArray.Length == 6 ? lineArray : new[] { "", "", "", "", "", "","" };  
        }  
      
        var list = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt"))  
            .Select((lineData) =>  
            {  
                Item item = new Item();  
                if (string.IsNullOrWhiteSpace(lineData)) return item;  
                var lineArray = LineArrayMethod(lineData);  
                item = new Item  
                {  
                    Column1 = lineArray[0],  
                    Column2 = lineArray[1],  
                    Column3 = lineArray[2],  
                    Column4 = lineArray[3],  
                    Column5 = lineArray[4],  
                    Column6 = lineArray[5],  
                    Column7 = lineArray[6]  
                };  
      
                return item;  
      
            })  
            // skip header  
            .Skip(1)  
            .ToList();  
      
      
        File.WriteAllLines("File1.txt", list.Select(x => x.Part1).ToArray());  
        File.WriteAllLines("File2.txt", list.Select(x => x.Part2).ToArray());  
        File.WriteAllLines("File3.txt", list.Select(x => x.Part3).ToArray());  
    }  
    

    Another example, there is a method that manipulates data several times in a method and we need to iterate the data for presentation and also update data dependent on business logic. So the main method is DeleteAndModifyRecordIndividualContexts and there are two local methods as per below.

    182266-f1.png

    0 comments No comments

0 additional answers

Sort by: Most helpful