Key Points About Local Functions
- You can use async and unsafe modifiers in local functions.
- Local functions have access to the container method's local variables, including method parameters.
- You are not permitted to use any member access modifiers, including the private keyword, in the local function definition since they are private, and you are not permitted to make them public.
- You may not additionally use the static keyword with a local function.
- It is also forbidden to apply attributes to the local function, its parameters, or its parameter type.
- It is possible to have many local functions.
- Local functions are not permitted to be overloaded.
Example
using System;
public class ExampleProgram {
// Main method
public static void Main()
{
// Local Function
void LocalFunction(int x, int y)
{
Console.WriteLine("Value of x is: " + x);
Console.WriteLine("Value of y is: " + y);
Console.WriteLine("Sum of x and y is: {0}", x + y);
Console.WriteLine();
}
// Calling Local function
LocalFunction(12, 10);
LocalFunction(40, 30);
}
}
Output
Value of x is: 12
Value of y is: 10
Sum of x and y is: 22
Value of x is: 40
Value of y is: 30
Sum of x and y is: 70
Advantages of Local Functions
Local generic functions are permitted.
Example
using System;
public class ExampleProgram {
// Main method
public static void Main()
{
// Local Function with out parameter
void LocalFunction(string str, out string s)
{
s = str + "Learn"
+ "Grow";
}
string x = null;
// Calling Local function
LocalFunction("Ninjas!!", out x);
Console.WriteLine(x);
}
}
Output
LearnGrowNinjas!!
You can use parameters in local functions.
Example
using System;
public class ExampleProgram {
public static void Main()
{
// Local Function
// Using params
void LocalFunctions(params string[] chars)
{
for (int x = 0; x < chars.Length; x++)
{
Console.WriteLine(chars[x]);
}
}
// Calling Local function
LocalFunctions("Learn", "Code", "CodingNinjas", "1234CN");
}
}
Output
Learn
Code
CodingNinjas
1234CN
Frequently Asked Questions
What are the different types of functions in C#?
Pure virtual method, Virtual method, Abstract method, Partial method, Extension method, Instance method, Static method.
Are functions called methods in C#?
To execute the function, you must call it in the Main() method after it has been created. To call a method, first, construct an object of the contained class, then call the method using the dot(.) operator. If a method is static, there is no need to build an object; simply call it followed by the class name.
What is the difference between function and method?
The phrases method and function are interchangeable. In object-oriented programming, a method is a process or function. A function is a collection of reusable code that may be invoked from anywhere in your application. This avoids the need to write the same code again and over.
Recommended Topic - singleton design pattern in c#
Conclusion
In this article, we have extensively discussed the Local functions in C# with examples.
We hope that this blog has helped you enhance your knowledge of the functions in C#, and if you would like to learn more about C#, check out our articles C# | Learn & Practice from Coding Ninjas Studio.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!