Some of the new features that are implemented in ASP.Net version 4.0 are
a)Optional parameters
We can implement a feature like function overloading using Optional parameter.
When we are declaring a function with optional parameters we don’t need to supply the argument for the optional parameter. If we do not supply its value the default value will be used else it will take the passed argument as its value. Optional parameter must be specified as the last parameter.
Sample Code :
class Program
{
static void Main (string[] args)
{
//Optional Parameter
//We can call function foo by passing values for only first parameter
int x = foo(5); // result will be 0. (5 * 0)
int y = foo(5,2); // result will be 10. (5 * 2)
}
public static int foo(int a, int b = 0)
{
return a * b;
}
}
b)Named Parameters
It gives us the flexibility in the order of the passed arguments. We need not consider the order of the parameters as we can give our own order by specifying the parameter name with the argument.
For example :
class Program
{
static void Main (string[] args)
{
string y = foo(b: "B", a: "A");// Output wil be B
}
public static string foo(string a, string b)
{
return b;
}
}
Its output will be "B".
c) Dynamic variables
We don't need to initialize dynamic variables. Compiler is not able to identify its type at runtime,
For Example :
dynamic d; // You don’t need to initialize the variable
d = "hi";
int Len = d.Hi();//This code compiles fine. But it will through an exception at runtime.
//when this line of code is executed the compiler will come to know its type and identifies that this object does not have a property called "Hi()"
//when this line of code is executed the compiler will come to know its type and identifies that this object does not have a property called "Hi()"

