Func and Action generic delegates and Local function in C# 7.0. The Func and Action are already available in C# while Local function is introduced in C# 7.0.
# | Func | Action | Local Function |
---|---|---|---|
1 | It is a generic delegate. | It is a generic delegate. | It is method which defined in another method body. |
2 | It has 0 to 16 parameters. | It has 0 to 16 parameters. | It has 0 to 32 parameters but it’s code smell with too many parameters. |
3 | It always returns a value. | It doesn’t return value. | It returns value that could be void. |
4 | It must be defined before its call. | It must be defined before its call. | It could be defined later in the method. |
Func
using static System.Console;
using System;
namespace PrimeNumberApp
{
class Program
{
static void Main(string[] args)
{
Func<int, bool> checkPrimeNumber = x =>
{
bool isPrime = false;
int i;
for (i = 2; i <= x - 1; i++)
{
if (x % i == 0)
{
isPrime = false;
break;
}
}
if (i == x)
{
isPrime = true;
}
return isPrime;
};
WriteLine("Enter a number");
int number = Convert.ToInt32(ReadLine());
bool isPrimeNumber = checkPrimeNumber(number);
WriteLine($"{number } is {(isPrimeNumber ? "" : "not")} prime number");
ReadKey();
}
}
}
Action
using System;
using static System.Console;
namespace FibonacciSeriesApp
{
class Program
{
static void Main(string[] args)
{
Action<int> GetFibonacciSeries = x =>
{
int starter = 0, next = 1, number = 0;
Write($"{starter} {next}");
for (int i = 2; i < x; i++)
{
number = starter + next;
Write($" {number}");
starter = next;
next = number;
}
};
WriteLine("Enter a length");
int num = Convert.ToInt32(ReadLine());
GetFibonacciSeries(num);
ReadKey();
}
}
}
Local Function
using System;
using static System.Console;
namespace LocalFunctionApp
{
class Program
{
static void Main(string[] args)
{
WriteLine("Enter a number to calculate factorial");
int num = Convert.ToInt32(ReadLine());
long fact = GetFactorial(num);
WriteLine($"{num} factorial is {fact}");
long GetFactorial(int number)
{
return number == 0 ? 1 : number * GetFactorial(number - 1);
}
ReadKey();
}
}
}
using static System.Console;
using System;
namespace PrimeNumberApp
{
class Program
{
static void Main(string[] args)
{
Func<int, bool> checkPrimeNumber = x =>
{
bool isPrime = false;
int i;
for (i = 2; i <= x - 1; i++)
{
if (x % i == 0)
{
isPrime = false;
break;
}
}
if (i == x)
{
isPrime = true;
}
return isPrime;
};
WriteLine("Enter a number");
int number = Convert.ToInt32(ReadLine());
bool isPrimeNumber = checkPrimeNumber(number);
WriteLine($"{number } is {(isPrimeNumber ? "" : "not")} prime number");
ReadKey();
}
}
}
Action
using System;
using static System.Console;
namespace FibonacciSeriesApp
{
class Program
{
static void Main(string[] args)
{
Action<int> GetFibonacciSeries = x =>
{
int starter = 0, next = 1, number = 0;
Write($"{starter} {next}");
for (int i = 2; i < x; i++)
{
number = starter + next;
Write($" {number}");
starter = next;
next = number;
}
};
WriteLine("Enter a length");
int num = Convert.ToInt32(ReadLine());
GetFibonacciSeries(num);
ReadKey();
}
}
}
Local Function
using System;
using static System.Console;
namespace LocalFunctionApp
{
class Program
{
static void Main(string[] args)
{
WriteLine("Enter a number to calculate factorial");
int num = Convert.ToInt32(ReadLine());
long fact = GetFactorial(num);
WriteLine($"{num} factorial is {fact}");
long GetFactorial(int number)
{
return number == 0 ? 1 : number * GetFactorial(number - 1);
}
ReadKey();
}
}
}
No comments:
Post a Comment