const Vs readonly Keyword and params
const-:
- A constant is a data member which is evaluated at Compile time.
- const is implicitly static.
readonly-:
- readonly data member is evaluated at run-time in its declaration or in constructor.
- it is not implicitly “Static” but can be “Static” or as per Instance.
- If we explicitly make “read-only” field static and know its value at compile time then that field works as const.
e.g.
// Same as public const double PI = 3.14;
public static readonly double PI = 3.14;
params-:
- params is mainly used to declare variable number of arguments inside the method called the parameter array.
- Also the argument with Prefix params must be the last argument inside the method.
Example-:
public int Sum(params int[] arr)
{
int sum=0;
foreach(int i in arr)
{
sum=sum+i;
}
return sum;
}
public static void main()
{
int sum = Sum(5,10,15);
Console.WriteLine(“Sum is {0}”,sum);
}

